template<typename T>
struct A
{
template<typename U>
A() {}
template<typename U>
static void f() {}
};
int main()
{
A<int>::f<int>(); // ok
auto a = A<int><double>(); // error C2062: type 'double' unexpected
}
问题在代码中是不言而喻的。
我的问题是:
如何调用模板类的模板构造函数?
最佳答案
您不能直接调用类的构造函数。如果您无法从调用中推断出构造函数的模板参数,则无法调用该特定构造函数。
您可以做的是创建某种可用于零开销扣除的类型包装器:
template <typename T>
struct type_wrapper { };
template<typename T>
struct A
{
template<typename U>
A(type_wrapper<U>) {}
};
int main()
{
auto a = A<int>(type_wrapper<double>{});
}
关于c++ - 如何调用模板类的模板构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43808848/