sashoalm
sashoalm

Reputation: 79595

Does the C++ standard allow a template constructor for a non-template class?

I want to create a class with a template constructor:

class foo
{
    template <class T> foo(T var) {}
};

This compiles in VS2008, but I have no idea if it is a non-standard extension, or if the C++ standard allows it.

Upvotes: 6

Views: 1452

Answers (1)

user634175
user634175

Reputation:

In [temp.mem]:

A template can be declared within a class or class template; such a template is called a member template.

Constructors are members and it isn't explicitly disallowed to make them templates.

For example, std::shared_ptr has many template constructors.

Upvotes: 6

Related Questions