Implicit Template Parameters Implicit Template Parameters xcode xcode

Implicit Template Parameters


The constructor could in theory infer the type of the object it is constructing, but the statement:

Foo MyFoo(123);

Is allocating temporary space for MyFoo and must know the fully-qualified type of MyFoo in order to know how much space is needed.

If you want to avoid typing (i.e. with fingers) the name of a particularly complex template, consider using a typedef:

typedef std::map<int, std::string> StringMap;

Or in C++0x you could use the auto keyword to have the compiler use type inference--though many will argue that leads to less readable and more error-prone code, myself among them. ;p


compiler can figure out template parameter type only for templated functions, not for classes/structs


Compiler can deduce the template argument such case:

template<typename T>void fun(T param){    //code...}fun(100);    //T is deduced as int;fun(100.0);  //T is deduced as doublefun(100.0f); //T is deduced as floatFoo<int> foo(100);fun(foo);    //T is deduced as Foo<int>;Foo<char> bar('A');fun(bar);    //T is deduced as Foo<char>;

Actually template argument deduction is a huge topic. Read this article at ACCU:

The C++ Template Argument Deduction