Template constructor in a class template - how to explicitly specify template argument for the 2nd parameter?(类模板中的模板构造函数 - 如何为第二个参数显式指定模板参数?)
问题描述
类模板中的模板构造函数 - 如何为第二个参数显式指定模板参数?
Template constructor in a class template - how to explicitly specify template argument for the 2nd parameter?
尝试为构造函数 2 显式指定模板参数时出现编译错误.如果我真的想显式调用构造函数 2 应该怎么做?
compile error when tried to explicit specify template argument for constructor 2. How should I do it if I really want to explicit call constructor 2 ?
请注意,当您要明确指定删除器类型时,这与 boost::shared_ptr 的情况相同.
Please note this is the same situation for boost::shared_ptr when you want to explicitly specify the deleter type.
注意对于非-构造函数foo(),明确指定工作正常.
N.B. For non-construction function foo(), explicitly specify works fine.
N.B 我知道它工作正常没有为构造函数 2 明确指定第二个作为模板参数推导通常工作正常,我只是好奇如何明确指定它.
N.B I know it works fine without specify the 2nd one explicitly for the constructor 2 as template argument deduction normally just works fine, I am just curious how to specify it explicitly.
template<class T> class TestTemplate {
public:
//constructor 1
template<class Y> TestTemplate(T * p) {
cout << "c1" << endl;
}
//constructor 2
template<class Y, class D> TestTemplate(Y * p, D d) {
cout << "c2" << endl;
}
template<class T, class B>
void foo(T a, B b) {
cout << "foo" << endl;
}
};
int main() {
TestTemplate<int> tp(new int());//this one works ok call constructor 1
//explicit template argument works ok
tp.foo<int*, string>(new int(), "hello");
TestTemplate<int> tp2(new int(),2);//this one works ok call constructor 2
//compile error when tried to explicit specify template argument for constructor 2
//How should I do it if I really want to explicit call constructor 2?
//TestTemplate<int*, int> tp3(new int(), 2); //wrong
//TestTemplate<int*> tp3<int*,int>(new int(), 2); //wrong again
return 0;
}
推荐答案
修复您的代码,以下内容将起作用:
Fixing your code, the following would work:
template<class T> class TestTemplate {
public:
//constructor 1
template<class Y> TestTemplate(Y * p) {
cout << "c1" << endl;
}
//constructor 2
template<class Y, class D> TestTemplate(Y * p, D d) {
cout << "c2" << endl;
}
template<class A, class B>
void foo(A a, B b) {
cout << "foo" << endl;
}
};
int main() {
TestTemplate<int> tp(new int());
tp.foo<int*, string>(new int(), "hello");
TestTemplate<int> tp2(new int(),2);
}
您不能将 T 用于类模板参数 和 构造函数模板参数.但是,要回答您的问题,来自 [14.5.2p5]:
You cannot use T for the class template parameter and the constructor template parameter. But, to answer your question, from [14.5.2p5]:
因为显式模板参数列表跟在函数后面模板名称,因为转换成员函数模板和调用构造函数成员函数模板时不使用函数名,无法提供显式模板这些函数模板的参数列表.
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.
因此,您不能为构造函数显式指定模板参数.
Therefore, you cannot explicitly specify template arguments for constructor.
这篇关于类模板中的模板构造函数 - 如何为第二个参数显式指定模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:类模板中的模板构造函数 - 如何为第二个参数显
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- CString 到 char* 2021-01-01
