The following code does not compile, and I just can't figure out why.
template <class T, class... Ts>
typename std::enable_if<sizeof...(Ts) > 0>::type func() {
// nop
}
The error message produced is:
error: expected unqualified-id before numeric constant
typename std::enable_if<sizeof...(Ts) > 0u>::type func() {
^
You need parentheses for this to be parsed correctly by the compiler:
template <class T, class... Ts>
typename std::enable_if<(sizeof...(Ts) > 0)>::type func() {
^ ^
// nop
}
The compiler interprets the right angle bracket (>
) as a closing bracket for std::enable_if
. This happens because once you begin a template parameter (or argument) list, the first time, the compiler, has the chance to close it (with >
), it does so.
Solution (that proves the above point): Don't close the parameter list, reverse the condition and use a left angle bracket:
template <class T, class... Ts>
typename std::enable_if< 0 < sizeof...(Ts) >::type func() {}
// ^ compilers are cool with this
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With