c++调用模板类的特定模板构造函数
如果类也是模板,是否可以使用模板参数调用构造函数?
#include <stdio.h>
#include <iostream>
template <class A>
struct Class
{
template <class B>
Class(B arg) { std::cout << arg << std::endl; }
};
int main()
{
Class<int> c<float>(1.0f);
Class<int>* ptr = new Class<int><float>(2.0f);
return 0;
}
编辑:所以我想调用特定模板构造函数的唯一方法是使用将参数转换为您想要的模板类型来调用它:
#include <stdio.h>
#include <iostream>
template <class A>
struct Class
{
template <class B>
Class(B arg) { std::cout << arg << std::endl; }
Class(double arg) { std::cout << "double" << std::endl; }
Class(float arg) { std::cout << "float" << std::endl; }
};
int main()
{
Class<int> c(1.0f);
Class<int>* ptr = new Class<int>((double)2.0f);
return 0;
}
// 这输出: 漂浮 double
edit2:但是不属于构造函数参数本身的构造函数模板参数会发生什么情况?
template <class B, class C>
Class(B arg) { /* how do you specify ?? C */ }
Is it possible to call a constructor with template arguments if the class is a template too?
#include <stdio.h>
#include <iostream>
template <class A>
struct Class
{
template <class B>
Class(B arg) { std::cout << arg << std::endl; }
};
int main()
{
Class<int> c<float>(1.0f);
Class<int>* ptr = new Class<int><float>(2.0f);
return 0;
}
edit: so I guess the only way to call a specific template constructor is to call it with casted paramterers to the template type you want:
#include <stdio.h>
#include <iostream>
template <class A>
struct Class
{
template <class B>
Class(B arg) { std::cout << arg << std::endl; }
Class(double arg) { std::cout << "double" << std::endl; }
Class(float arg) { std::cout << "float" << std::endl; }
};
int main()
{
Class<int> c(1.0f);
Class<int>* ptr = new Class<int>((double)2.0f);
return 0;
}
// this outputs:
float
double
edit2: but what happens to constructor template arguments that are not part of the constructor arguments itself ?
template <class B, class C>
Class(B arg) { /* how do you specify ?? C */ }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您提供的示例中,您实际上不需要显式给出
template
参数来调用构造函数,例如:简单地提供参数为
1.0f
足够:同样的事情也适用于
new
示例。话虽如此,我认为您不能使用template
参数显式调用构造函数(与普通函数不同)。In the example you gave you actually don't need to explicitly give the
template
argument to invoke constructor like:Simply providing argument as
1.0f
is enough:Same thing is applicable for
new
example too. Having said that, I don't think constructor you can invoke explicitly usingtemplate
argument (unlike normal function).然后你可以传入一个已编码的参数
Then you can pass-in an argument that has it encoded
您需要显式声明类本身的模板类型(在您的示例中为
A
)。但您无需说明B
是什么类型。编译器从您传递1.0f
时知道B == float
。构造函数调用中没有任何内容可以帮助编译器确定A
是什么,因此您必须告诉它:You need to explicitly state the template type that goes with the class itself (that's
A
in your example). But you don't need to say what typeB
is. The compiler knows from you passing1.0f
thatB == float
. There's nothing in the constructor call to help the compiler figure out whatA
is, so you have to tell it:这就够了。它将调用具有模板参数
float
的构造函数。编译器将从参数1.0f
推导出构造函数模板的类型参数。由于1.0f
是float
,因此编译器将推导出的类型参数是float
。同样参见这些:
This is enough. It will invoke the constructor which has template argument
float
. From the argument1.0f
, the compiler will deduce the type argument of the constructor template. Since1.0f
isfloat
, so the type argument the compiler will deduce isfloat
.Likewise see these: