28

I want to only define a function based on the size of the template parameter:

template <class T>
typename std::enable_if<sizeof(T) > 1, void>::type
foobify(T v) {
    // ...
}

int main() {
    //foobify((unsigned char)30); // should not compile
    foobify((long)30);
}

However, I get:

main.cpp:8:41: error: expected unqualified-id before numeric constant
     typename std::enable_if<sizeof(T) > 1, void>::type

It works if I instead do 1 < sizeof(T). Thus I believe GCC is thinking I am ending the template parameter, instead of continuing the boolean expression.

Is there any way to use > itself without having to work around it?

Claudiu
  • 224,032
  • 165
  • 485
  • 680

3 Answers3

33

Yes, expressions using that operator must be parenthesized. See [temp.names]/3:

When parsing a template-argument-list, the first non-nested >138 is taken as the ending delimiter rather than a greater-than operator. [..] [ Example:

template<int i> class X { /* ...*/ };

X< 1>2 > x1; // syntax error
X<(1>2)> x2; // OK

— end example ]

138) A > that encloses the type-id of a dynamic_cast, static_cast, reinterpret_cast or const_cast, or which encloses the template-arguments of a subsequent template-id, is considered nested for the purpose of this description.

Clearly that doesn't apply if you use the symmetric counterpart of that comparison, i.e. employing <, instead - parsing is unambiguous in that case.

Columbo
  • 60,038
  • 8
  • 155
  • 203
14

Yes, you should use parentheses:

template <class T>
typename std::enable_if<(sizeof(T) > 1), void>::type
foobify(T v) {
    // ...
}
Claudiu
  • 224,032
  • 165
  • 485
  • 680
1

For C++20,

template<class T>
requires (sizeof(T) > 1)
void foobify(T v) {
    // ...
}

int main() {
//  foobify((unsigned char)30); // should not compile
    foobify((long)30);
}
Ping Chia
  • 61
  • 4