C++ templates are generally used for generics (that is, one code is shared by multiple types), but they can also be used for non-type template parameters, which can be integers, characters, Boolean, etc.Equivalent types can be clearly determined through literals.
You usually declare a type template parameter as template <typename T>, but here the Non-type template parameter is template <bool B>. This is the only difference. If we want to talk about enable_if this template technique in detail, the key is not in Non-type or partial spec, but in SFINAE. You should learn this aspect.
C++ templates are generally used for generics (that is, one code is shared by multiple types), but they can also be used for non-type template parameters, which can be integers, characters, Boolean, etc.Equivalent types can be clearly determined through literals.
You usually declare a type template parameter as
template <typename T>
, but here the Non-type template parameter istemplate <bool B>
. This is the only difference. If we want to talk aboutenable_if
this template technique in detail, the key is not in Non-type or partial spec, but in SFINAE. You should learn this aspect.enable_if says template<bool _test>, so naturally you have to fill in true/false.
When using it, you will find that if the result of an expression expr is true, then enable_if<expr>::type exists. So you can write code like this:
to test whether a type is no less than 4 bytes. If it is returned as is, a compilation error will occur if it is not.