【问题标题】:namespace in debug flags of in-class defined friend functions类内定义的友元函数的调试标志中的命名空间
【发布时间】:2019-02-07 16:30:13
【问题描述】:

我正在处理一个在没有外部声明的类中定义友元函数的类

namespace our_namespace {
template <typename T>
struct our_container {

  friend our_container set_union(our_container const &, our_container const &) {
    // meaningless for the example here, just a valid definition
    // no valid semantics
    return our_container{};
  }
};
}  // namespace our_namespace

如前所述(例如herehere),函数set_union 不在our_namespace 命名空间中,但将通过参数相关查找找到:

auto foo(std::vector<our_namespace::our_container<float>> in) {
  // works:
  return set_union(in[0], in[1]);
}

但我注意到 in the debug flags set_union 似乎在 our_namespace 命名空间中

        mov     rdi, qword ptr [rbp - 40] # 8-byte Reload
        mov     rsi, rax
        call    our_namespace::set_union(our_namespace::our_container<float> const&, our_namespace::our_container<float> const&)
        add     rsp, 48
        pop     rbp
        ret
our_namespace::set_union(our_namespace::our_container<float> const&, our_namespace::our_container<float> const&): # @our_namespace::set_union(our_namespace::our_container<float> const&, our_namespace::our_container<float> const&)
        push    rbp
        mov     rbp, rsp
        mov     qword ptr [rbp - 16], rdi
        mov     qword ptr [rbp - 24], rsi
        pop     rbp
        ret

虽然我不能称它为our_namespace::set_union

auto foo(std::vector<our_namespace::our_container<float>> in) {
  // fails:
  return our_namespace::set_union(in[0], in[1]);
}

关于如何理解调试信息的任何提示?

编辑:set_union 函数体在这里只是一个稻草狗示例,具有有效定义。

【问题讨论】:

    标签: c++ argument-dependent-lookup friend-function


    【解决方案1】:

    C++ 标准仅定义了与代码编译和生成程序的行为有关的编译器行为。它没有定义代码生成的所有方面,特别是,它没有定义调试符号。

    因此,您的编译器正确(根据标准)不允许通过它不在的命名空间调用该函数。但是由于该函数确实存在并且您应该能够对其进行调试,因此它需要将调试符号放在某处。封闭命名空间似乎是一个合理的选择。

    【讨论】:

    • 嗯,由this answer 触发,我查看了中间编译器步骤here,看起来set_unionour_namespace 中。我很困惑。
    • @pseyfert 不会将实现细节与正式语言规则混淆。规则是在命名空间中找不到名称(仅通过 ADL 查找),因此从代码的角度来看,它不在命名空间中。但是,名称存在,编译器需要生成符号和所有内容,并将符号放入代码生成透视图的命名空间中。
    猜你喜欢
    • 1970-01-01
    • 2020-09-19
    • 2012-06-11
    • 2012-06-11
    • 2013-05-19
    • 1970-01-01
    • 2016-12-12
    • 2014-08-12
    • 1970-01-01
    相关资源
    最近更新 更多