3

Having issues finding a syntax for hoisting constrained member function outside of its class that GCC is happy with. At this point I'm starting to think it's a GCC bug.

struct S {
    template<typename Idx>
    static constexpr bool some_check = true;

    template<typename Idx>
    requires some_check<Idx>
    void foo(Idx);
};

template<typename Idx>
requires S::template some_check<Idx>
void S::foo(Idx idx) {}

Clang accepts it but not GCC:

$ g++ -std=c++20 test.cpp
test.cpp:12:6: error: no declaration matches ‘void S::foo(Idx)’
   12 | void S::foo(Idx idx) {}
      |      ^
test.cpp:7:10: note: candidate is: ‘template<class Idx>  requires  some_check<Idx> void S::foo(Idx)’
    7 |     void foo(Idx);
      |          ^~~
test.cpp:1:8: note: ‘struct S’ defined here
    1 | struct S {
      |        ^

Is this program well-formed?

9
  • FWIW: clang started to accept it only from ver 21, and latest gcc and msvc reject it. See demo. But I'm not sure which is right. Commented Nov 26 at 16:25
  • Workaround: use S::template some_check in both places. Or S::some_check, as some_check isn't a dependant name. Commented Nov 26 at 16:29
  • 1
    GCC is fine when the spelling is exactly the same godbolt.org/z/o91WsEede (or the link in Caleth's comment). So it's almost certainly a GCC bug where it thinks the requires clauses are actually different when they're just syntactically different. Commented Nov 26 at 16:31
  • 1
    template is needed when S would depend on template parameter and it is impossible to tell what S::some_check is. So dropping this keyword fixes this, but also scope operator should be added in declaration: godbolt.org/z/WE9cMMox6 Apparently scope operator in declaration fixes all combinations. Commented Nov 26 at 16:33
  • 1
    You can use this form void foo(Idx) requires some_check<Idx> so that no need to spell S::template some_check. godbolt.org/z/fjMxKfGsj Commented Nov 26 at 16:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.