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.
这篇关于类模板中的模板构造函数 - 如何为第二个参数显式指定模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:类模板中的模板构造函数 - 如何为第二个参数显


基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01