我有以下代码的问题:
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/