模板构造函数的奇怪之处
可能的重复:
可以显式指定构造函数的模板参数吗?< /a>
跟进我之前的问题, (我在编辑2中发现了这种情况)
代码简单:
#include <iostream>
struct Printer
{
Printer() { std::cout << "secret code" << std::endl; }
};
template <class A>
struct Class
{
template <class B, class C>
Class(B arg)
{
C c; /* the 'secret code' should come from here */
std::cout << arg << std::endl;
}
Class(double arg) { std::cout << "double" << std::endl; }
Class(float arg) { std::cout << "float" << std::endl; }
/* this forbids the use of printer in the first parameter */
Class(Printer printer) { throw std::exception(); /* here be dragons */ }
};
int main()
{
Class<int> c(1.0f);
Class<int>* ptr = new Class<int>((double)2.0f);
return 0;
}
// Can anyone print 'secret code' while creating an object of type 'Class' ?
详细:对于模板构造函数,当实例化对象时,您可以指定一个不属于构造函数参数的模板参数吗?
我认为这本身就值得一个问题。
Possible Duplicate:
Can the template parameters of a constructor be explicitly specified?
following up on my previous question, (I found this situation in edit 2)
Laid out simple in code:
#include <iostream>
struct Printer
{
Printer() { std::cout << "secret code" << std::endl; }
};
template <class A>
struct Class
{
template <class B, class C>
Class(B arg)
{
C c; /* the 'secret code' should come from here */
std::cout << arg << std::endl;
}
Class(double arg) { std::cout << "double" << std::endl; }
Class(float arg) { std::cout << "float" << std::endl; }
/* this forbids the use of printer in the first parameter */
Class(Printer printer) { throw std::exception(); /* here be dragons */ }
};
int main()
{
Class<int> c(1.0f);
Class<int>* ptr = new Class<int>((double)2.0f);
return 0;
}
// Can anyone print 'secret code' while creating an object of type 'Class' ?
Detailed: For a template constructor, can you specify a template argument which is not part of the constructor's arguments when an object get's instantiated?
I think this deserves a question of its own.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这是不可能的。
没有任何语法可以为构造函数模板提供显式模板参数。您只能为整个类模板提供显式模板参数。
以下来自
[temp.arg.explicit]
的文本(2003 年措辞,14.8.1/5)涵盖了该场景。尽管该子句是非规范性的,但它向我们解释,作为语法的固有限制,这是不可能的:这在一定程度上是由于您自己从未真正显式调用构造函数这一事实。例如,当您编写
A()
时,您并没有像函数一样调用构造函数,即使它看起来好像是(“调用转换成员函数模板和构造函数成员函数模板时不使用函数名称”)。No, it's not possible.
There is no syntax with which you can provide explicit template parameters to a constructor template. You can only provide explicit template parameters for the class template as a whole.
The following text from
[temp.arg.explicit]
(2003 wording, 14.8.1/5) covers the scenario. Though the clause is non-normative, it serves to explain to us that, as an inherent restriction of the grammar, this is not possible:This, partially, comes out of the fact that you never actually invoke the constructor explicitly yourself. When you write, say,
A()
you are not calling the constructor like a function, even though it looks as if you are ("conversion member function templates and constructor member function templates are called without using a function name").我想他想知道如何用 C 实例化这个类作为 SomeType:
我不知道这是否可能。
I think he want to know how to instantiate this class with C as SomeType:
I don't know if this is possible.