簡體   English   中英

范圍解析運算符和從屬名稱

[英]Scope resolution operator and dependent name

我有以下測試代碼

#include <iostream>
template <typename T>

struct PS
{
   template <typename U>
   static void foo()
   {
       std::cout<<"Some test code";
   }
};

template <typename T>
void bar()
{
   PS<T>::template foo<T>(); //won't compile without `::template`
}

int main()
{
   bar<int>();
}

ISO C ++ 03 14.2/4 :說

當成員模板專業化的名稱出現之后。 或 - >在postfix-expression中,或在qualified-id中的nested-name-specifier之后,postfix-expression或qualified-id顯式依賴於template-parameter(14.6.2),成員模板名稱必須是以關鍵字模板為前綴。 否則,假定該名稱命名非模板

標准談到->. 但不是關於:: 它是C ++ 03標准中的缺陷還是我遺漏了什么? 有人請賜教。

但是, N3126的措辭已經改變

當成員模板專業化的名稱出現之后 或 - >在post fi x-expression中或在quali fi ed-id中的嵌套名稱指定符之后,post fi x-expression的對象或指針表達式或者qual-fi-id中的嵌套名稱-peci fi er取決於模板參數(14.6.2) 但不引用當前實例化的成員(14.6.2.1),成員模板名稱必須由關鍵字模板預先設定。 否則,假定該名稱命名非模板。

有人可以給出一個例子來說明在C ++ 0x的上下文中but does not refer to a member of the current instantiation什么but does not refer to a member of the current instantiation嗎?

-PS

標准談到->. 但不是關於::

范圍解析運算符( :: )是合格-ID的部分提到了“或在合格-ID 嵌套名稱說明符后”。

C ++ 0x中的附加措辭是CWG缺陷224的解決方案的一部分。 實際上,依賴名稱的定義已經改變:

關於名稱是依賴還是非依賴的決定應該基於查找,而不是基於名稱的形式:如果名稱可以在定義上下文中查找而不能作為專業化的結果,則名稱應該是非依賴的。

有人可以給出一個例子來說明“但是沒有引用當前實例化的成員”意味着在C ++ 0x的上下文中嗎?

我不知道這是否實際上在C ++ 03和C ++ 0x的實現之間表現不同。

template< typename Q >
struct A {
    template< typename T >
    void f( T );

    void g() {
        this->f< Q >( 5 ); // member of current instantiation is not ambiguous

        A< identity<Q> >().template f< 5 >(); // suppose A is partially 
// specialized on identity. Legal but confusing; need help from template keyword.
    }
};

暫無
暫無

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

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