xml*_*lmx 4 c++ syntax templates constructor type-deduction
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
}
Run Code Online (Sandbox Code Playgroud)
这个问题在代码中是不言而喻的.
我的问题是:
如何调用模板类的模板ctor?
您不能直接调用类的构造函数.如果您无法从调用中推断出构造函数的模板参数,则无法调用该特定构造函数.
你可以做的是创建一些类型的包装器,可以用于零开销扣除:
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>{});
}
Run Code Online (Sandbox Code Playgroud)