可以有一个模板化的构造函数,如'template< class T> X(){}'? [英] Is it possible to have a templated constructor like 'template<class T> X(){}'?

查看:103
本文介绍了可以有一个模板化的构造函数,如'template< class T> X(){}'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct X{
    template<class T>
    X(){}
};

是否可以实例化这样的类型?

Is it possible to instantate such a type?

推荐答案

是的,有可能有这样的构造函数,但是不可能调用它。模板化构造函数的所有模板参数必须从参数列表中推导出或具有默认值。在您的示例中,您无法实例化该类。

Yes, it is possible to have such a constructor, but it's impossible to call it. All the template parameters of a templated constructor must be deduced from the parameter list or have a default value. In Your example you can't instantiate the class.

[temp.mem]

[temp.mem]


[注意:因为显式模板参数列表跟在函数模板名称
之后,并且因为转换成员函数模板和构造函数成员函数模板被称为
而不使用函数名,所以没有办法为这些
函数模板提供一个显式的模板参数列表。 -end note]

[ 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. —end note ]

这篇关于可以有一个模板化的构造函数,如'template&lt; class T&gt; X(){}'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆