c++ - 为什么 using 指令不是 "associate"与普通函数?

标签 c++ namespaces

根据 this questionusing 指令之后定义类方法是有效的,而不是将它们包含在 namespace block 中。

然而,对于普通函数来说,情况似乎并非如此。考虑:

问候语.hh

#pragma once

namespace NS
{
    class Greeting
    {
    public:
        void hello();
    };

    void otherHello();
}

问候语.cc

#include "Greeting.hh"
#include <iostream>

using namespace NS;

void Greeting::hello()
{
    std::cout << "Greeting::hello" << std::endl;
}

void otherHello()
{
    std::cout << "otherHello" << std::endl;
}

main.cc

#include "Greeting.hh"

int main()
{
    NS::Greeting o;
    o.hello();
    NS::otherHello();
}

这不会编译,产生以下错误消息:

undefined reference to `NS::otherHello()'

进一步检查表明 otherHello 的符号前面没有命名空间,而 Greeting::hello 的是:

g++ -std=c++14 -pedantic -Wall -c Greeting.cc
nm -C Greeting.o | grep T
000000000000002a T otherHello()
0000000000000000 T NS::Greeting::hello()

这是否与 accepted answer 中的标准引用相矛盾? ?

"During unqualified name lookup (3.4.1), the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace."

最佳答案

重要的是要记住

  1. 不同命名空间中的函数声明不会相互干扰。
  2. 函数的定义也是声明。
  3. [namespace.def/4]

The enclosing namespaces of a declaration are those namespaces in which the declaration lexically appears, except for a redeclaration of a namespace member outside its original namespace (e.g., a definition as specified in [namespace.memdef]). Such a redeclaration has the same enclosing namespaces as the original declaration.

那么让我们看一下otherHello 的定义。它在词汇上出现在哪里?当然在全局命名空间中。这也是它的声明点。这意味着封闭的命名空间是全局命名空间,您最终会得到 ::otherHello 的声明。

所以不,这与其他问题的已接受答案的标准引述并不矛盾。成员函数可以在类之外定义,只要它们由类名 ( [class.mfct/4] ) 限定即可:

If the definition of a member function is lexically outside its class definition, the member function name shall be qualified by its class name using the ​::​ operator.

那么我们只需要问一下,GreetingNS::Greeting 是同一个类吗?为什么,是的。 using 指令对此负责。


我将添加此部分以希望得到澄清。考虑这个代码片段:

namespace NS1 {
    namespace NS2 {
        void hello();
    }
}

using namespace NS1;

void NS2::hello() {

}

int main() {
    NS1::NS2::hello();
    return 0;
}

当编译器遇到正在定义的 NS2::hello 时,它会为该声明符 ID 执行名称查找。根据[basic.lookup.qual/3] :

In a declaration in which the declarator-id is a qualified-id, names used before the qualified-id being declared are looked up in the defining namespace scope; names following the qualified-id are looked up in the scope of the member's class or namespace.

因此 NS2 在定义范围(全局范围)中查找,根据您引用的非限定名称查找规则,找到并解析为 NS1::NS2。这就是 NS2::helloNS1::NS2::hello 关联并解析为定义它的方式。

在 OP 的全局命名空间中,otherHello 前面没有任何内容。因此不会进行名称查找。正如我之前引用的那样,它会立即在封闭的命名空间中定义该函数。

关于c++ - 为什么 using 指令不是 "associate"与普通函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44994461/

相关文章:

c++ - if (intVar) 和 if (intVar != 0) 之间有区别吗?

c++ - BidirectionalIterator 的 decltype(*it) 是什么?

c++ - "more than one instance of overloaded function "标准::战俘 "matches the argument list"

php - 我必须在 Laravel 中使用什么命名空间或外观才能访问命名空间类中的模型?

c++ - 当外部命名空间具有同名成员时访问未命名命名空间的成员

c++ - 带有自定义类的 std::atomic (C++ 11)

c++ - 模板推导中的const T&和T&有什么区别

c++ - Centos 7安装HHVM报错"Internal compiler"

java - Java 中包相对标识符路径

c# - 类在同一命名空间中找不到另一个类