C++ 使用来自 CRTP 模板-模板库的构造函数 Class
C++ Using Constructors from CRTP Template-Template Base Class
从 curiously recursing template pattern (CRTP) 模板-模板基础 class 继承构造函数的语法是什么?
template<typename T, template<typename> typename U>
struct Base {
Base(int) { }
};
template<typename T>
struct Derived : public Base<T, Derived> {
using Base<T, Derived>::Base;
};
int main(int argc, char *argv[]) {
Derived<double> foo(4);
return 0;
}
在VS2019中,上述代码导致如下错误:
error C3210: 'Base<double, Derived<double> >': a member using-declaration can only be applied to a base class member
error C3881: can only inherit constructor from direct base
使上述代码工作所需的语法是什么?
这是有效代码,而且 does compile with GCC 9.2。尝试更新版本的编译器或不同的编译器。或联系您的实施者。
在那之前,这里是 does compile with MSVC 现在的解决方法:
template<typename T, template<typename> typename U>
struct Base {
Base(int) { }
};
template<typename T>
struct Derived;
template<typename T>
using Base_type = Base<T, Derived>;
template<typename T>
struct Derived : public Base_type<T> {
using Base_type<T>::Base;
};
int main() {
Derived<double> foo(4);
return 0;
}
从 curiously recursing template pattern (CRTP) 模板-模板基础 class 继承构造函数的语法是什么?
template<typename T, template<typename> typename U>
struct Base {
Base(int) { }
};
template<typename T>
struct Derived : public Base<T, Derived> {
using Base<T, Derived>::Base;
};
int main(int argc, char *argv[]) {
Derived<double> foo(4);
return 0;
}
在VS2019中,上述代码导致如下错误:
error C3210: 'Base<double, Derived<double> >': a member using-declaration can only be applied to a base class member
error C3881: can only inherit constructor from direct base
使上述代码工作所需的语法是什么?
这是有效代码,而且 does compile with GCC 9.2。尝试更新版本的编译器或不同的编译器。或联系您的实施者。
在那之前,这里是 does compile with MSVC 现在的解决方法:
template<typename T, template<typename> typename U>
struct Base {
Base(int) { }
};
template<typename T>
struct Derived;
template<typename T>
using Base_type = Base<T, Derived>;
template<typename T>
struct Derived : public Base_type<T> {
using Base_type<T>::Base;
};
int main() {
Derived<double> foo(4);
return 0;
}