[英]Define default implementation of template class in C++
I know it is possible to define default values for template parameters: 我知道可以为模板参数定义默认值:
template<int N = 10> struct Foo {};
You can use this like Foo<>
for example, but I want to be able to write just Foo
. 例如,您可以像
Foo<>
这样使用它,但我希望只写Foo
。
I tried the following, but it doesn't work (throws compiler exception): 我尝试了以下操作,但是不起作用(抛出编译器异常):
struct Foo : Foo<10> {};
Is this possible in C++? 这在C ++中可能吗?
You can't directly, but can achieve something close thanks to a typedef
, ie 您不能直接使用,但是可以通过
typedef
接近实现,即
template<int N = 10> struct Foo{};
typedef Foo<> DefaultFoo;//Or whatever name that fits, just not 'Foo'
int main() {
DefaultFoo myFoo;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.