灵感来自这个答案中的代码.考虑:
template<class>
class A { };
int main()
{
A<float> a(A<float>::A<int>());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是代码
A<float>::A格式错误,因为命名构造函数(根据§3.4.3.1[class.qual]/p2)并且不能在此上下文中使用(加上<int>完全无法解析),或者A<float>::A作为注射类名,用作模板的名称(§14.6.1[temp.local]),使得A<float>::A<int>装置完全一样A<int>,并且a被声明为函数(由于最烦恼的解析)?g ++说1. clang说2,ICC 13也是如此.哪个编译器正确?
只是好奇,是否有可能从模板类继承并在派生类的构造函数中调用基类的构造函数,该构造函数也是模板化的并且没有参数来推断其类型?
template<typename T>
struct Base {
template<typename D>
Base() { // no argument of type D to infer from
static_assert(std::is_same<T,D>::value, "");
}
};
struct Derived : Base<int> {
Derived() : Base<int>::Base<int>() {} // is there a way to write it correctly?
};
Run Code Online (Sandbox Code Playgroud)
在我的特定情况下,我可以用模板方法替换模板构造函数,但这仍然是一个关于语言灵活性的有趣问题。