Shane McMahon
Shane McMahon

Reputation: 21

No deduction in a 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 the type T be deduced from the argument 12 automatically?

Upvotes: 2

Views: 132

Answers (1)

Prasoon Saurav
Prasoon Saurav

Reputation: 92864

Template argument deduction applies only to function and member function templates but not to class templates. So your code is ill-formed.

You need to provide the template argument explicitly.

A<int> object(12); //fine

Upvotes: 4

Related Questions