[英]using with class & namespace differencies/ambiguity
我只是好奇为什么用using指令设计它。 对于1)struct被视为命名空间和2)它不是:
struct foo
{
using type0 = int;
};
namespace bar
{
using type1 = int;
}
using bar::type1;
using type0 = foo::type0; // 1)
using foo::type0; // 2)
clang version 3.3 (branches/release_33 186829)
clang -std=c++11 test.cpp
test.cpp:13:12: error: using declaration can not refer to class member
using foo::type0;
gcc version 4.8.1
c++ -std=c++11 test.cpp
test.cpp:13:12: error: ‘foo’ is not a namespace
using foo::type0;
类不是名称空间; 他们有严格的范围。 类成员的名称(在类外部访问时)必须始终以类名作为前缀。
using
不允许更改。
#1工作的原因是因为您为类中声明的类型创建了一个类型别名 。 这就是using name = typename;
确实。 在这种情况下,它与typedef
没有什么不同。
#2不会创建别名; 期望在命名空间中为该语法指定一个名称以引入当前命名空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.