C++ 基于模板参数的不同类实现
我想这对于了解模板的人来说是微不足道的 假设我们需要此模板类的两种不同实现,具体取决于N的值:C++ 基于模板参数的不同类实现,c++,templates,c++11,template-meta-programming,C++,Templates,C++11,Template Meta Programming,我想这对于了解模板的人来说是微不足道的 假设我们需要此模板类的两种不同实现,具体取决于N的值: template <int N> class Foo { ... }; 模板 福班{ ... }; 例如: template <int N> class Foo { ... // implementation for N <= 10 }; template <int N> class Foo { ... // implementa
template <int N>
class Foo {
...
};
模板
福班{
...
};
例如:
template <int N>
class Foo {
... // implementation for N <= 10
};
template <int N>
class Foo {
... // implementation for N > 10
};
模板
福班{
…//n10的实现
};
在C++11中如何做到这一点?使用带有默认值的额外模板参数来区分情况:
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
};
使用
std::condition的模板感谢您:1。回答简洁且非常有用,2。不要问“你为什么要这么做?”。