Search code examples
c++c++11variadic-templatestemplate-meta-programmingenable-if

Using sizeof... within std::enable_if


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() {
                                         ^

Solution

  • 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
    }