Desmond Gold
Desmond Gold

Reputation: 2085

Operator '>' or '>>' bug from GCC inside requires expression within template argument C++

I'm testing these requirements that compose of two basic operations for > or >> inside the template argument if these expressions are valid:

template <typename T>
struct dummy 
    : std::bool_constant<
    requires (const T& a, const T& b) {
     a > b;
     a >> b;
}> {};

With Clang, it compiles fine. But with GCC, it gave me these several errors:

<source>:7:7: error: expected ';' before '>' token
    7 |      a > b;
      |       ^~
      |       ;
<source>:8:7: error: expected ';' before '>>' token
    8 |      a >> b;
      |       ^~~
      |       ;

So, in order to suppress these errors, I have to cover the expressions with parenthesis:

template <typename T>
struct dummy 
    : std::bool_constant<
    requires (const T& a, const T& b) {
     (a > b);
     (a >> b);
}> {};

And now, GCC compiles fine.

Even so, this problem may be similar to the thing with C<42, sizeof(int) > 4>, but in this case, operations with >> and > have been enclosed with braces from requires expression requires (...) { ... }

Unfortunately, I can't file a bug on GCC because I don't have an account yet nor registered.

Upvotes: 2

Views: 133

Answers (1)

Afshin
Afshin

Reputation: 9173

It seems that you have found a workaround yourself, but I found if you pull out requires expression as a concept, the code will compile too:

template<typename T>
concept MyConcept=requires (const T& a, const T& b) {
     a > b;
     a >> b;
};

template <typename T>
struct dummy : std::bool_constant<MyConcept<T>> {};

Maybe this is my personal preference, but I think the code written like this increases readability too.

Upvotes: 3

Related Questions