Alex Rivera | Logout

When a compiler can infer a template parameter?

Asked 2009-04-28T12:21:11.667
40

Sometimes it works sometimes not:

template <class T> 
void f(T t) {}

template <class T>
class MyClass {
public:
  MyClass(T t) {}
};

void test () {
  f<int>(5);
  MyClass<int> mc(5);
  f(5);
  MyClass mc(5); // this doesn't work
}

Is there a way to hack around the example above? I.e. force the compiler to infer the template parameter from constructor parameter.

Will this be fixed in the future, or is there a good reason not to?

What is the general rule when compiler can infer template parameter?

Edit
Report

1 Answer

8
answered 2009-04-28T12:30:56.167

Your Answer