<分区>
Possible Duplicate:
Can the template parameters of a constructor be explicitly specified?
跟进我之前的 question , (我在edit 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' ?
详解:对于模板构造函数,是否可以在实例化对象时指定一个不属于构造函数参数的模板参数?
我认为这值得单独提出一个问题。