Conditionally resolving constructor

Here's how I can conditionally enable the class constructor:

struct Foo { template<class T> Foo( T* ptr, boost::enable_if<is_arithmetic<T> >::type* = NULL ) {} }; 

I would like to know why I need to make an inclusion using a dummy parameter. Why can't I just write:

 struct Foo { template<class T> Foo( boost::enable_if<is_arithmetic<T>, T>::type* = NULL ) {} }; 
+7
source share
1 answer

You can use any function other than the constructor, because then you can change the name of the function, including the template arguments.

 Foo foo; foo.Method<T>(); 

Together with the constructor, the name of the constructor never appears in your expression, so there is no room for explicitly placing the template parameter. You must infer it from the argument.

 Foo<T> foo; // no good, assumes the type Foo is a template, not its constructor Foo* pfoo = new Foo<T>(); // same problem Foo foo((T*)0); // ok, deduced Foo* pfoo = new Foo((T*)0); // same 
+8
source

All Articles