dshin
dshin

Reputation: 2428

Concept specifying reference argument of method

The following code does not compile:

struct FooImpl {
   using Bar = int;
   void method(Bar&) {}
 };
 
 template <typename F>
 concept Foo = requires(F f) {
   { f.method(typename F::Bar{}) };
 };
 
 static_assert(Foo<FooImpl>);

The problem comes from the & in the FooImple::method() declaration. Removing the & fixes the problem.

Instead of changing FooImpl, I would like to change Foo. How can I change the concept implementation to satisfy the static_assert?

Upvotes: 0

Views: 374

Answers (1)

Barry
Barry

Reputation: 303537

If the purpose of the concept is to see if F has a member function named method that can accept an lvalue of type F::Bar, then what you want to write is:

template <typename F>
concept Foo = requires (F f, typename F::Bar arg) {
    f.method(arg);
};

This will gracefully fail if there is no typename F::Bar also (i.e. Foo<int> is false, not ill-formed).

Upvotes: 2

Related Questions