c++ - 使用指令和范围

标签 c++ c++11 templates namespaces name-lookup

我有以下代码的问题:

struct A
{
    A(int i) {};
};

namespace Foo
{
using Alias = A;
}

struct B : Foo::Alias
{
    B();
};

B::B() : Alias(5)
{}

int main()
{
    return 0;
}
它不会编译,因为在 B 构造函数中,我没有精确定义 Alias 的范围。
为什么 ?
谢谢。

最佳答案

来自 cppreference在命名空间页面上:

inline(optional) namespace attr(optional) identifier { namespace-body }


The namespace-body defines a namespace scope, which affects name lookup. All names introduced by the declarations that appear within namespace-body (including nested namespace definitions) become members of the namespace identifier, whether this namespace definition is the original namespace definition (which introduced identifier), or an extension namespace definition (which "reopened" the already defined namespace)


因此,根据标准所提到的内容,在命名空间内定义为命名空间主体的任何内容都被视为该命名空间的范围和 qualified name lookup适用于引用命名空间内的任何内容。

关于c++ - 使用指令和范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62578456/

相关文章:

c++ - 将程序一的输出作为程序二的输入传递

c++ - 条件运算符 + upcast + const 引用

c++ - 通过模板的监听器模式,如何在不指定模板参数的情况下使用模板类?由 小码哥发布于

c++ - G++ 6.1.0 中可能的回归

c++ - 来自 "check if member exists using enable_if"的修改代码不起作用

c++ - 编译器错误没有意义(错误是缺少函数参数)

c++ - 尝试扩展模板参数包时出错

C++,函数将数组作为字符串返回

c++ - 如何在 native nodejs 插件中使用环境变量作为编译时常量?

C++:Ray Tracer 输出中的非确定性行为