Can the template parameters of a constructor be explicitly specified?(可以显式指定构造函数的模板参数吗?)
问题描述
类的构造函数可以是模板函数.在调用此类构造函数时,编译器通常会查看提供给构造函数的参数,并从中确定使用的模板参数.是否也有一些语法可以明确指定模板参数?
A constructor of a class can be a template function. At the point where such a constructor is called, the compiler usually looks at the arguments given to the constructor and determines the used template parameters from them. Is there also some syntax to specify the template parameters explicitly?
一个人为的例子:
struct A {
template<typename T>
A() {}
};
有没有办法实例化这个类?显式指定构造函数的模板参数的语法是什么?
Is there a way to instantiate this class? What is the syntax to explicitly specify the constructor's template parameters?
我的用例将是一个问题,因为编译器似乎没有找到正确的模板化构造函数.显式指定模板参数可能会生成更有用的错误消息,甚至可以解决问题.
My use case would be a problem were the compiler doesn't seem to find the correct templated constructor. Explicitly specifying the template parameters would probably generate more useful error messages or even resolve the problem.
推荐答案
没有.C++03 标准说:
No. The C++03 standard says:
[注意:因为显式模板参数列表跟在函数模板名称之后,并且因为转换成员函数模板和构造函数成员函数模板是在不使用函数名的情况下调用的,所以无法为这些函数模板提供显式的模板参数列表.] (§14.5.2/5)
[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.] (§14.5.2/5)
这篇关于可以显式指定构造函数的模板参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:可以显式指定构造函数的模板参数吗?
基础教程推荐
- CString 到 char* 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
