簡體   English   中英

C ++使用別名訪問嵌套類型(使用vs typedef)

[英]C++ accessing nested type using alias (using vs typedef)

今天我們發現了令人困惑的C ++ 11別名聲明行為。 這是一個例子:

template<typename T>
struct Q
{
    typedef T t;
};

template<typename T>
void foo(Q<T> q)
{
    using q_t = Q<T>; 
    //typedef Q<T> q_t; // if we uncomment this and comment 'using' the example compiles
    typename q_t::t qwe; // <<<<<<<<<< Error: no type named ‘t’ in ‘using q_t = struct Q<T>’
}

int main(int argc, char *argv[])
{
    Q<int> q;
    foo(q);
    return 0;
}

ISO 14882(C ++ 11)說這兩個聲明必須具有相同的語義(第145頁)。

但是,如果我們使用'using'聲明q_t,則示例不使用GCC 4.7.2(Debian Wheezy)和GCC 4.7.3(Ubuntu 13.04)進行編譯,但是使用'typedef'語句替換'using'語句會使其編譯。

這是GCC中的錯誤還是我們誤解了標准?

這似乎是一個GCC 4.7錯誤。

這是我編譯代碼的測試 ,它使用gcc 4.8.1

所以規范說:

using q_t = Q<T>;
// is equivalent to this 
typedef Q<T> q_t;

使用--std = c ++ 11使用g ++ 4.8.1編譯好

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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