我想创建一个带有模板构造函数的类:
class foo
{
template <class T> foo(T var) {}
};
这在 VS2008 中编译,但我不知道它是否是非标准扩展,或者 C++ 标准是否允许它。
最佳答案
在 [temp.mem] 中:
A template can be declared within a class or class template; such a template is called a member template.
构造函数是成员,并没有明确禁止将它们设为模板。
例如,std::shared_ptr
有很多 template constructors .
关于c++ - C++ 标准是否允许非模板类的模板构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20161535/