专门用于布尔值的真假

Specializing both true and false for bool

本文关键字:布尔值 用于      更新时间:2023-10-16

为什么C++允许对truefalse参数进行类型专用化?

template<bool> struct omg { /* can't access anything declared here */ };
template<> struct omg<true> { };
template<> struct omg<false> { };

是否有任何情况是有意义/有用的?

我认为

没有这种情况。但标准对模板非类型参数没有任何限制,即适合条件。顺便说一下,这样的限制应该是,它应该适用于所有类型。做这样的事情是不正确的

enum A { first, second };
template<A> struct omg {};
template<> struct omg<first> {};
template<> struct omg<second> {};

恕我直言,这太复杂了。