use*_*866 7 c++ templates template-meta-programming c++11
我认为这对于了解模板的人来说是微不足道的......
假设我们想要这个模板类的两个不同的实现,具体取决于N的值:
template <int N>
class Foo {
...
};
Run Code Online (Sandbox Code Playgroud)
例如:
template <int N>
class Foo {
... // implementation for N <= 10
};
template <int N>
class Foo {
... // implementation for N > 10
};
Run Code Online (Sandbox Code Playgroud)
我们怎样才能在C++ 11中做到这一点?
Jes*_*der 20
使用带有默认值的额外模板参数来区分大小写:
template <int N, bool b = N <= 10>
class Foo;
template <int N>
class Foo<N, true> {
... // implementation for N <= 10
};
template <int N>
class Foo<N, false> {
... // implementation for N > 10
};
Run Code Online (Sandbox Code Playgroud)