c++ - GCC 还是 MSVC,谁是正确的?

标签 c++ visual-studio gcc namespaces name-lookup

假设我们有这样的结构:

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;
}

[basic.lookup.unqual]/8 :

For the members of a class X, a name used [...] in the definition of a class member outside of the definition of X, 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 of X ([class.member.lookup]), or

  • [...two bullet points about nested and local classes omitted...]

  • if X is a member of namespace N [...], before the use of the name, in namespace N or in one of N's enclosing namespaces.

类型的名称查找应首先在bar内部查找,然后在classes内部查找,然后在some_namespace内部查找。最后一个应该找到命名空间types

关于c++ - GCC 还是 MSVC,谁是正确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40674779/

相关文章:

c++ - 尝试包装 native C++ 类时警告 C4150 删除指向不完整类型的指针

python - 项目文件行抑制状态错误LNK1104无法打开文件 'python310_d.lib'

C++11:GCC 4.8 静态 thread_local std::unique_ptr 未定义引用

c# - 在设计时限制用户控件的大小

windows - IIS Express 8 与 IIS

c - GCC编译错误类型冲突

c - 是否有 GCC 关键字允许结构重新排序?

c++ - 如何将 boost::asio::write 的缓冲区存储为shared_ptr?

php - 如何在 PHP 中优化 Dijkstra 代码?

c++ - 处理包含和使用标题