Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template disambiguator

I'm trying to find any information about template keyword used as disambiguator, but there is nothing about that. Probably I'm searching wrong keywords, but there is nothing like .template or ->template in standard. Google shows only GCC problems from different forums, but not really explanation what is it used for.

Code like that failed to compile without template keyword on line 11 (on GCC), but I'm not quite sure that this conforms standard.

template<typename B>
struct S1
{
    template<typename T> void test() {}
};

template<typename T>
struct S2
{
    S2()
    {
        S1<T>().template test<int>();
    }
};

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

So my question is: why does template keyword used here, what kind of ambiguity is there without that keyword and where can I read about that (I would really appreciate link to standard).

Thanks.

like image 462
confucius Avatar asked Nov 02 '10 11:11

confucius


1 Answers

Short answer : Because the standard says so

ISO C++03 14.2/4

When the name of a member template specialization appears after . or -> in a postfix-expression, or after nested-name-specifier in a qualified-id, 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.

P.S:

Without that extra use of template, the compiler does not know that the less-than token (<) that follows is not really "less than" but the beginning of a template argument list.

like image 98
Prasoon Saurav Avatar answered Oct 27 '22 00:10

Prasoon Saurav