Can C ++ constructors be templates?

I have a class without a template with a template constructor. This code compiles for me. But I remember that somewhere I mentioned that constructors cannot be templates. Can anyone explain if this is a legitimate use?

typedef double Vector; //enum Method {A, B, C, D, E, F}; struct A {}; class Butcher { public: template <class Method> Butcher(Method); private: Vector a, b, c; }; template <> Butcher::Butcher(struct A) : a(2), b(4), c(2) { // a = 0.5, 1; // b = -1, 1, 3, 2; // c = 0, 1; } 

Thanks, Gokul.

+7
c ++ constructor templates
source share
2 answers

Yes, constructors can be templates.

+12
source share

This is absolutely true for designers who are members of the template. The only thing I can think of what you might think is that the template constructor is never a copy constructor, so the template constructor alone will not prevent the compiler from creating the created copy constructor.

+13
source share

All Articles