开发者

Scope resolution operator and dependent name

I have the following test code

#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: says

When the name of a member template specialization appears after . or -> in a postfix-expression, or after nested-name-specifier in a qualified-i开发者_Python百科d, and the postfix-expression or qualified-id explicitly depends on a template-parameter (14.6.2), the member template name must be prefixed by the keyword template. Otherwise the name is assumed to name a non-template.

The Standard talks about -> and . but not about ::. Is it a defect in the C++03 Standard or am I missing something? Someone please enlighten me.

However the wording has been changed in N3126

When the name of a member template specialization appears after . or -> in a postfix-expression or after a nested-name-specifier in a qualified-id, and the object or pointer expression of the postfix-expression or the nested-name-specifier in the qualified-id depends on a template parameter (14.6.2) but does not refer to a member of the current instantiation (14.6.2.1), the member template name must be prefixed by the keyword template. Otherwise the name is assumed to name a non-template.

Can someone give an example to illustrate what but does not refer to a member of the current instantiation means in context of C++0x?

-PS


The Standard talks about -> and . but not about ::.

The scope resolution operator (::) is part of the qualified-id referred to by "or after nested-name-specifier in a qualified-id."

The additional verbiage in C++0x is part of the resolution to CWG defect 224. Effectively, the definition of dependent names has been changed:

The decision on whether a name is dependent or non-dependent should be based on lookup, not on the form of the name: if the name can be looked up in the definition context and cannot be anything else as the result of specialization, the name should be non-dependent.


Can someone give an example to illustrate what "but does not refer to a member of the current instantiation" means in context of C++0x?

I don't know if this is something that in fact behaves differently between an implementation of C++03 and 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.
    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜