No deduction in class template

template<typename T> class A { public: A(T b) : a(b) { } private: T a; }; A object(12); //Why does it give an error? 

Why can't type T be automatically inferred from argument 12 ?

+7
c ++ templates
source share
1 answer

The output of the template argument applies only to function and member templates, and not to class templates. Thus, your code is poorly formed.

You need to provide the template argument explicitly.

 A<int> object(12); //fine 
+4
source share

All Articles