簡體   English   中英

使用類和名稱空間差異/歧義

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM