Template arguments ( ISO/IEC 14882:2011(E) § 14.3) [temp.arg]
2 In a template-argument, an ambiguity between a type-id and an expression is resolved to a type-id, regardless of the form of the corresponding template-parameter.
[ Example:
-
// template_argument.cpp
-
//
-
#include <iostream>
-
using namespace std;
-
-
template<class T> void f()
-
{
-
cout<<"[template void f()] called\n"<<endl;
-
}
-
-
template<int I> void f()
-
{
-
cout<<"[template void f()] called\n"<<endl;
-
}
-
-
class Functor
-
{
-
public:
-
int operator()(void)
-
{
-
cout<<"[int Functor::operator()(void)] called\n"<<endl;
-
return true;
-
}
-
};
-
-
int g()
-
{
-
return true;
-
}
-
-
int main(int argc, char* argv[])
-
{
-
cout<<"running [f();]"<<endl;
-
f<int()>(); // int() is a type-id: call the firt template<class T> void f()
-
-
cout<<"running [f();]"<<endl;
-
f<Functor()>();
-
-
cout<<"running [f();]"<<endl;
-
f<int>();
-
-
cout<<"running [f<1>();]"<<endl;
-
f<1>();
-
-
cout.flush();
-
return 0;
-
}
Result:
-
running [f<int()>();]
-
[template<class T> void f()] called
-
-
running [f<Functor()>();]
-
[template<class T> void f()] called
-
-
running [f<int>();]
-
[template<class T> void f()] called
-
-
running [f<1>();]
-
[template<int I> void f()] called
— end example ]
There is no such ambiguity in a default template-argument because the form of the template-parameter determines the allowable forms of the template-argument.
阅读(1039) | 评论(0) | 转发(0) |