与类和命名空间一起使用 差异/歧义
using with class & namespace differencies/ambiguity
我只是好奇为什么用using指令来设计它。对于1)结构被视为命名空间,而对于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不创建别名;该语法期望在名称空间内被赋予一个名称,以将其引入当前名称空间。
相关文章:
- 构造对象的歧义
- 将两个数组中的差异记录在第三个数组中
- 大小相等但成员数量不同的结构之间的性能差异
- C++constexpr实现差异
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 旧版c++中结构和类之间的差异
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 节点 *temp; 和节点 *tmp = 新节点之间的差异
- GCC 和 Clang 代码性能的巨大差异
- 类内部和外部静态 constexpr 元组之间的差异
- 平凡类型与非平凡类型的复制消除差异
- 了解算法的性能差异(如果以不同的编程语言实现)
- 以天C++为单位的两个时间戳之间的差异
- 数组初始值设定项的构造函数歧义
- 如何以毫秒为单位获取开始时间和 now() 之间的毫秒差异(以 C++为单位?
- 方括号 [] 和括号 () 之间的犰狳库差异
- 不同语言中的模运算符差异
- 消除好友和成员二进制运算符的歧义
- 用' . '代替' :: '会在C++中造成歧义吗?
- 与类和命名空间一起使用 差异/歧义