3

I would like the template method m in the concept D to be restricted by another concept E, something like this:

template <typename t>
concept E = requires(t p_t) {
  std::default_initializable<t>;
  std::copy_constructible<t>;
  std::move_constructible<t>;

  typename t::x;
  requires std::is_convertible_v<typename t::x, int>;
};

template <typename t>
concept D = requires(t p_t) {

  { template <E e> p_t.m<E>() } -> std::same_as<float>;
};

but the line { template <E e> p_t.m<E>() } -> std::same_as<float>; does not compile, it generates the error "Expected expression".

If I understood right what is discussed here, it is not possible, but I wonder if someone else knows a way to do it.

Aamir
  • 1,974
  • 1
  • 14
  • 18
canellas
  • 491
  • 5
  • 17
  • Can you explain in plain English what are the requirements for types that match concept D? – n. m. could be an AI Jul 08 '23 at 17:08
  • 1
    Note that `std::default_initializable` in the `requires`-clause will always be satisfied, since it only checks the validity of the expression. [gcc-trunk](https://godbolt.org/z/q793hfTW4) also gives warnings about this. – 康桓瑋 Jul 08 '23 at 17:14
  • It is not possible to check at compile time that some function has a particular constraint. However, you could try checking that the function is *not* instantiable with some type template argument that doesn't satisfy the constraint. – Brian Bi Jul 08 '23 at 17:17
  • For the presented problem, it does not matter what `D` or `E` are, they can be anything anyone thin it is interested, they are meaningless abstractions. The core is how to use a previous defined `concept` in a template method defined in another `concept`. – canellas Jul 08 '23 at 17:29
  • 2
    Note I did not ask what D or E mean. I asked to formulate requirements for types that match D in terms of E. For example, "the type must have a member named m which matches concept E" (I have no idea if that's what you really want). – n. m. could be an AI Jul 08 '23 at 18:33
  • It is not important for the question – canellas Jul 08 '23 at 18:34
  • Of course it is vitally important to the question. Without this information there is no question. "How do I use X" is not an answerable question. "How do I use X to do Y" is. But whatever floats your boat. – n. m. could be an AI Jul 08 '23 at 20:27
  • It is a question of abstraction. The role o `D` and `E` are not relevant in this case, that is why I used meaningless letters instead of concrete types. – canellas Jul 08 '23 at 22:31

0 Answers0