C++ 类模板中的模板构造函数-如何为第二个参数显式指定模板参数?
类模板中的模板构造函数-如何为第二个参数显式指定模板参数 尝试为构造函数2显式指定模板参数时出现编译错误。 如果我真的想显式调用构造函数2,我应该怎么做 请注意,当您希望显式指定删除器类型时,boost::shared_ptr的情况与此相同 注意:对于非构造函数foo(),请明确指定“精细工程” 注意:我知道它可以正常工作没有为构造函数2显式指定第二个参数,因为模板参数推导通常可以正常工作,我只是好奇如何显式指定它C++ 类模板中的模板构造函数-如何为第二个参数显式指定模板参数?,c++,templates,C++,Templates,类模板中的模板构造函数-如何为第二个参数显式指定模板参数 尝试为构造函数2显式指定模板参数时出现编译错误。 如果我真的想显式调用构造函数2,我应该怎么做 请注意,当您希望显式指定删除器类型时,boost::shared_ptr的情况与此相同 注意:对于非构造函数foo(),请明确指定“精细工程” 注意:我知道它可以正常工作没有为构造函数2显式指定第二个参数,因为模板参数推导通常可以正常工作,我只是好奇如何显式指定它 template<class T> class TestTempla
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;
}
template类TestTemplate{
公众:
//构造器1
模板测试模板(T*p){
cout您可以显式地为调用foo
指定模板参数,因为那些成员函数foo
有名称,而模板参数是该名称的一部分
这不适用于构造函数,因为构造函数没有名称。您不能(直接)调用构造函数。创建对象时当然会调用构造函数,但调用是生成的代码。修复代码后,以下操作将起作用:
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);
}
template类TestTemplate{
公众:
//构造器1
模板测试模板(Y*p){
cout不能显式指定构造函数的模板参数,因为构造函数本身没有名称,因此没有语法
但是,您可以通过以下方式确保推断出正确的模板参数:
- 转换实际参数,和/或
- 如有必要,引入“人工”额外参数只是为了携带类型信息,和/或
- 使用工厂函数
例如,您可以定义
template< class Type > struct TypeCarrier{ typedef Type T; };
struct MyClass
{
template< class Type >
MyClass( TypeCarrier< Type > ) { ... }
};
...
MyClass o( TypeCarrier<int>() );
templatestruct TypeCarrier{typedef Type T;};
结构MyClass
{
模板<类类型>
MyClass(TypeCarrier){…}
};
...
MyClass o(类型载体());
但不要被这些技巧冲昏头脑
相反,如果突然需要显式指定构造函数模板参数,请考虑设计是否真正合理
如果您考虑一下它的用途,也许可以使用一些更简单的设计?tempate TestTemplate(T*p)
导致一个错误(gcc 4.6.3):“错误:阴影模板parm'class T'。与void foo()相同)
。当我用例如X
替换T
时,我可以让它工作。你在使用哪个编译器?我甚至不能编译你的code@gogoprog你不是唯一一个。gcc说明了一切,VC10又被证明是愚蠢的!-1这个答案在“不能调用构造函数”之前是正确的胡说八道。这是一个老的初学者迷因,讨论可以追溯到几十年前。很难根除。但是从标准中查找默认构造函数的定义开始。是的,我知道我可以用foo来做这件事。请看上面的N.B。但我想为我的构造函数2做这件事&构造函数也有名字,它是类的名称……你是说它不能吗关于我的问题,你可以这样做了吗?@Gob00st:你不能,因为它没有名字。听理查德·韦克曼在他的“1984”专辑中的录音。注意我说的是“直接”,阿尔夫:你不能直接调用构造函数。你怎么能这样做?构造函数没有名字。@Gob00st-12.1,第1段以“构造函数没有名字”开头第2段继续说“因为构造函数没有名称,所以在名称查找过程中永远找不到它们;但是使用函数表示法的显式类型转换将导致调用构造函数来初始化对象。”感谢这个漂亮的元技巧。不,这不是设计问题,只是尝试了一些东西。