Error using C++20 requires clause on class template member function

Hi,

The C++20 code below fails to compile with nvc++ 22.2. It seems to concern the use of the requires clause, which distinguishes the two conversion functions. MFE code is shown below:

template <bool B>
struct Foo {                                                                    
  operator unsigned() const requires(B)  { return B; }                          
  operator unsigned() const requires(!B) { return B; }
};

int main(int argc, char *argv[])
{
  Foo<true>  f1;
  Foo<false> f2;

  unsigned i1 = f1;
  unsigned i2 = f2;

  return (i1==1 && i2==0) ? 0 : -1;
}

The invocation and error message are:

$ nvc++ -std=c++20 requires.cpp 
"requires.cpp", line 12: error: more than one conversion function from "Foo<true>" to "unsigned int" applies:
            function "Foo<B>::operator unsigned int() const [with B=true]" (declared at line 3)
            function "Foo<B>::operator unsigned int() const [with B=true]" (declared at line 4)
    unsigned i1 = f1;
                  ^

"requires.cpp", line 13: error: more than one conversion function from "Foo<false>" to "unsigned int" applies:
            function "Foo<B>::operator unsigned int() const [with B=false]" (declared at line 3)
            function "Foo<B>::operator unsigned int() const [with B=false]" (declared at line 4)
    unsigned i2 = f2;
                  ^

2 errors detected in the compilation of "requires.cpp".

Best regards,
Paul

Thanks Paul. I wrote an issue report, TPR #31370, and sent it to engineering for investigation.

-Mat

1 Like