代码之家  ›  专栏  ›  技术社区  ›  Prasoon Saurav

作用域解析运算符和从属名称

  •  4
  • Prasoon Saurav  · 技术社区  · 15 年前

    我有以下测试代码

    #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

    当成员模板专用化的名称出现时 之后。或-gt; 在后缀表达式中,或在限定ID中嵌套名称说明符之后,后缀表达式或限定ID显式依赖于模板参数(14.6.2),成员 模板名称必须以关键字template作为前缀。否则,假定该名称命名非模板 .

    标准中提到 -> . 但不是关于 :: . 它是C++ 03标准中的缺陷还是我遗漏了什么?有人请开导我。

    然而,措辞在 N3126

    当成员模板专用化的名称出现在 . 或-gt; 在post_x表达式中或在限定ID中的嵌套名称之后指定,以及在限定ID中的post_x表达式或嵌套名称指定的对象或指针表达式取决于模板参数(14.6.2)。 但不引用当前实例化的成员 (14.6.2.1),成员模板名称必须为 由关键字模板预处理。否则,将假定该名称命名非模板。

    有人能举个例子来说明 but does not refer to a member of the current instantiation 在C++ 0x上下文中的方法?

    -PS

    2 回复  |  直到 15 年前
        1
  •  6
  •   James McNellis    15 年前

    标准中提到 -> . 但不是关于 :: .

    作用域解析运算符( :: )是 合格身份证 指“或之后” 嵌套名称说明符 在一个 合格身份证 ."

    C++0x中的附加冗长词是分辨率的一部分。 CWG defect 224 . 实际上,从属名称的定义已更改:

    决定一个名称是从属的还是非从属的,应该基于查找,而不是基于名称的形式:如果名称可以在定义上下文中查找,并且由于专门化而不能是任何其他名称,则该名称应该是非从属的。

        2
  •  3
  •   Potatoswatter    15 年前

    有人举一个例子来说明在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.
        }
    };