1

Is there a way to have a concept function signature that has a template argument?

Something like this:

template<typename SomeTypeT, typename U>
concept SomeType = requires(SomeTypeT s) {
    { s.SomeFunction<U>() };
};

?

Shout
  • 338
  • 5
  • 14
  • You have a concept and its template parameter having the same name `SomeTypeT`. This is not a good idea. See also [this](https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords). – n. m. could be an AI Jan 06 '22 at 16:51
  • Right. My bad. concept name should be `SomeType` instead of `SomeTypeT`. I have just fixed it. – Shout Jan 06 '22 at 17:00

1 Answers1

3

The shown concept definition works, except that you need to tell the compiler that SomeFunction is a template:

template<typename SomeTypeT, typename U>
concept SomeType = requires(SomeTypeT s) {
    { s.template SomeFunction<U>() };
};

This is always necessary if you want to reference a template member of a dependent name in a template definition, not specific to concepts. When parsing the definition, the compiler doesn't know yet what type s is and so can't know that SomeFunction is supposed to be a template. But it needs to know, since the meaning of the angle brackets and the whole expression can dependent on it.

user17732522
  • 53,019
  • 2
  • 56
  • 105