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 templ开发者_如何转开发ate-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?
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() { /* ... */ }
精彩评论