C++ 调用显式模板构造函数

2021-12-13 00:00:00 templates c++

你能告诉我如何显式调用模板构造函数(在初始化列表中)?例如:

Can you tell me how to invoke template constructor explicitly (in initializer list)? for example:

struct T { 
    template<class> T();
};

struct U {
    U() : t<void>() {} //does not work
    T t;
};

谢谢

推荐答案

这是不可能的.标准在 14.8.1/7

It's not possible. The Standard also has a note on this at 14.8.1/7

[注:因为显式模板实参列表跟在函数模板名之后,并且因为转换成员函数模板和构造函数成员函数模板是在不使用函数名的情况下调用的,所以没有办法为这些提供显式模板实参列表功能模板.]

[Note: because the explicit template argument list follows the function template name, and because conversion member function templates and constructor member function templates are called without using a function name, there is no way to provide an explicit template argument list for these function templates. ]

说明:这表示:模板参数在函数模板名称后的尖括号中传递,例如std::make_pair.并且构造函数没有自己的名字,但是他们在各种上下文中滥用他们的类名(所以 U() 的意思是:Pass 到类模板U,并通过调用不带参数的默认构造函数来构造一个对象).因此,不能将模板参数传递给构造函数.

Explanation: This says: Template arguments are passed in angle brackets after a function template name, such as std::make_pair<int, bool>. And constructors don't have a name of their own, but they abuse their class names in various contexts (so U<int>() means: Pass <int> to the class template U, and construct an object by calling the default constructor without arguments). Therefore, one cannot pass template arguments to constructors.

就您而言,您正试图在成员初始值设定项中传递模板参数.在这种情况下,还有更多问题:它会尝试将 t 解析和解释为基类类型,并认为您要调用基类的默认构造函数.这当然会失败.

In your case, you are trying to pass template arguments in a member initializer. In that case, there's even more of a problem: It will attempt to parse and interpret t<void> as a base-class type and thinks you want to call the default constructor of a base class. This will fail, of course.

如果你能忍受它,你就可以解决它

If you can live with it, you can work it around

struct T { 
    template<class U> T(identity<U>);
};

struct U {
    U() : t(identity<void>()) {}
    T t;
};

给定 identity 就像它在 boost 中定义的

Given identity like it's defined in boost

template<typename T> struct identity { typedef T type; };

在 C++20 中,您可以使用 std::type_identity 作为标识类型.

Within C++20 you can use std::type_identity as identity type.

  • https://en.cppreference.com/w/cpp/types/type_identity

相关文章