假设我们有这样的结构:
namespace some_namespace::types {
using foo_t = int;
}
namespace some_namespace::classes {
class bar {
public:
auto do_stuff() -> types::foo_t;
};
}
using namespace some_namespace::classes;
auto bar::do_stuff() -> types::foo_t {
return 1;
}
这段代码可以在 GCC6 中顺利编译。
另一方面,启用了 /std:c++latest
开关的 VS15 无法识别 do_stuff
的返回类型。它返回 C2653 .
现在,我发现非常可疑的是,通过将后半部分更改为以下内容可以解决此问题:
using namespace some_namespace;
auto classes::bar::do_stuff() -> types::foo_t {
return 1;
}
在我看来这应该是平等的。我是否错误地认为这是一个 MSVC bug?标准对此有何规定?
最佳答案
这与 C++17 功能无关,它不涉及 using 指令,实际上也根本不涉及 using 指令。 MSVC 产生相同的错误
namespace some_namespace{
namespace types {
using foo_t = int;
}
namespace classes {
class bar {
public:
auto do_stuff() -> types::foo_t;
};
}
}
using some_namespace::classes::bar;
auto bar::do_stuff() -> types::foo_t {
return 1;
}
For the members of a class
X
, a name used [...] in the definition of a class member outside of the definition ofX
, following the member's declarator-id, shall be declared in one of the following ways:
before its use in the block in which it is used or in an enclosing block ([stmt.block]), or
shall be a member of class
X
or be a member of a base class ofX
([class.member.lookup]), or[...two bullet points about nested and local classes omitted...]
if
X
is a member of namespaceN
[...], before the use of the name, in namespaceN
or in one ofN
's enclosing namespaces.
类型的名称查找应首先在bar
内部查找,然后在classes
内部查找,然后在some_namespace
内部查找。最后一个应该找到命名空间types
。
关于c++ - GCC 还是 MSVC,谁是正确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40674779/