Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should the default template-argument of a mem func be explicitly defined as illegal?

In N3126 (Warning: very large PDF) 14.1/9, there are two statements that make me confused:

#1: "A default template-argument may be specified in a template declaration."

#2: "A default template-argument shall not be specified in the template-parameter-lists of the definition of a member of a class template that appears outside of the member’s class."

#1 means the following code is legal:

template <class T = int>
void f(T = T())
{}

int main()
{
    int n = f(); // equivalent to f<int>() or f(0);
    return 0;
}

#2 means the following code is illegal:

template <class T>
struct X
{
    template <class U = T>
    void f(U a = U())
    {}
};

int main()
{
    X<int> x;
    x.f(); // illegal, though I think it should be equivalent to x.f<int>() or x.f(0)
    return 0;
}

I just wonder why the latter should be explicitly defined as illegal by the standard?

What is the rationale?

like image 411
xmllmx Avatar asked Oct 13 '22 20:10

xmllmx


1 Answers

I might be mistaken, but my understanding of #2 is that it makes the following illegal :

template<class T>
struct A
{
    void foo();
};

template<class T = int>
void A<T>::foo() { /* ... */ }
like image 84
icecrime Avatar answered Nov 15 '22 08:11

icecrime