3

Suppose I have a concept Concept and a bunch of similar types that provide a member function template as follows:

class Typical {
  template<Concept T>
  auto something() { return ...; }
};

How do I write a concept HasSomething that tests for the existence of such a member template function? Note that the function template has no arguments and is designed to be called as obj.something<T>(). I have to test whether the function is callable with any Concept.

Edit: all the approaches I can think of require to at least provide an instance of T. I can imagine how to test whether Typical::template something is well-formed if something where supposed to be a class template, but not for a function template.

gigabytes
  • 3,104
  • 19
  • 35
  • Did you try to write one? What didn't work? – Barry Jun 06 '22 at 19:03
  • I added something to the question. The problem is I don't know how to write a trait that tests whether `Typical::template something` is well-formed without calling it *e.g.* inside `std::void_t`, but then I would have to chose a type to call it with. – gigabytes Jun 06 '22 at 19:06
  • There's no way to avoid choosing the argument type. – HolyBlackCat Jun 06 '22 at 19:08
  • 1
    You don't test for the existence of a function with a signature. You test for whether an expression is valid. What expression are you going to use in the template that this concept will be applied to? Put that in the `requires` expression, along with whatever template parameters it requires. – Nicol Bolas Jun 06 '22 at 19:09
  • @NicolBolas thanks but that would not work in my case. I've edited the question to better align with the real case. I do not have an argument to give to the function to deduce the template parameter. I have to test whether the member function is callable with some type that satisfies `Concept`. But I cannot select one to use for the test, and even if I could, the test would still only test for the expression to be well-formed for that specific type. – gigabytes Jun 06 '22 at 19:13
  • 1
    @gigabytes: At some point, you are going to write template code that does this: `value.template something()`, yes? At that point, you have `SomeType`. So put that into the concept, and have your function require that `SomeType` fulfill whatever `Concept` is in addition to your desired concept. – Nicol Bolas Jun 06 '22 at 19:36
  • To add to what @NicolBolas is saying: If you don't have `SomeType` in your codebase because you're making some library, you can create a sample `SomeType` in a test file in some test project. – AndyG Jun 06 '22 at 19:42
  • You probably want to do [something like this](https://stackoverflow.com/a/53598655/2069064) but it's still hard for me to tell what you're asking. – Barry Jun 06 '22 at 19:43

0 Answers0