有没有办法在编译期限制值类型模板参数的取值范围?
//要求这个a不能大于20?template<int a>class C{};static_assert( a <= 20 , "a must not be larger than 20");
[解决办法]
楼主,还是多花点精力在学习真正问题的解决吧,玩高阶模板基本是浪费生命。
如果你真好奇心够强,去买本《modern c++ design》啃啃。
[解决办法]
#1 的方法挺好,如果悲催的 VS2010 不认的话,还可以使用下面的方法。
template <bool> struct base;template <> struct base<true> { };template <int a> struct c : private base<(a<=20)> { };
[解决办法]
模板(元)编程挺有用的,况且学习它也不妨碍学习别的东西。楼主安心学好了自然能够体会其中乐趣,何必在意他人看法呢。Modern C++ Design 是本好书,值得细读。
[解决办法]
C++是无底洞,见好就收把。
[解决办法]
static_assert正解
VC2010非常支持
LZ继续努力 给你泼冷水的多是自己不太明白的