C++ invoke explicit template constructor(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
这篇关于C++ 调用显式模板构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 调用显式模板构造函数


基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01