9

I have a generic function which can accept types which have at least one of a list of member functions.

I am trying to write an inline requires expression to validate this.

I have an example below which fails to compile because neither of my types pass the requires clause

#include <iostream>

struct Foo
{
    bool has_foo() const { return true; }
};

struct Bar
{
    bool has_bar() const { return true; }
};


template<typename T>
bool check(const T& t)
    requires requires (T t)
    {
        { t.has_foo() || t.has_bar() };
    }
{
    if constexpr (requires { t.has_foo(); })
    {
        if (t.has_foo())
            std::cout << "has foo\n";
        else
            std::cout << "no foo\n";
    }
    if constexpr (requires { t.has_bar(); })
    {
        if (t.has_bar())
            std::cout << "has bar\n";
        else
            std::cout << "no bar\n";
    }
    return true;
}

int main()
{
    check(Foo());
    check(Bar());
    return 0;
}

The build fails due to:

test.cpp:40:10: error: no matching function for call to 'check(Foo)'
   40 |     check(Foo());
      |     ~~~~~^~~~~~~
test.cpp:15:6: note: candidate: 'template<class T> bool check(const T&) requires requires(T t) {{t.has_foo() || t.has_bar()};}'
   15 | bool check(const T& t)
      |      ^~~~~
test.cpp:15:6: note:   template argument deduction/substitution failed:
test.cpp:15:6: note: constraints not satisfied
test.cpp: In substitution of 'template<class T> bool check(const T&) requires requires(T t) {{t.has_foo() || t.has_bar()};} [with T = Foo]':
test.cpp:40:10:   required from here
test.cpp:15:6:   required by the constraints of 'template<class T> bool check(const T&) requires requires(T t) {{t.has_foo() || t.has_bar()};}'
test.cpp:16:14:   in requirements with 'T t' [with T = Foo]
test.cpp:18:23: note: the required expression '(t.has_foo() || t.has_bar())' is invalid, because
   18 |         { t.has_foo() || t.has_bar() };
      |           ~~~~~~~~~~~~^~~~~~~~~~~~~~
test.cpp:18:28: error: 'struct Foo' has no member named 'has_bar'
   18 |         { t.has_foo() || t.has_bar() };
      |                          ~~^~~~~~~

How can I turn my requires expression

requires (T t)
{
    { t.has_foo() || t.has_bar() };
}

into a logical or expression, such that types which meet either of the expressions are allowed?

3
  • 1
    Interesting, haven't been aware that concepts can be used in if constexpr... Always learning something new ;)
    – Aconcagua
    Mar 21 at 10:08
  • @Aconcagua yeah, can make for really clean generic code! Mar 21 at 10:44
  • @Aconcagua: learned this trick yesterday form video: 'C++ Weekly - Ep 368 - The Power of template-template Parameters: A Basic Guide' (youtube.com/watch?v=s6Cub7EFLXo)
    – gast128
    Mar 21 at 11:13

2 Answers 2

8

You can split it into two requires expressions, which can be combined with logical OR:

template<typename T>
bool check(const T& t)
    requires(
        requires (T t) { t.has_foo(); } ||
        requires (T t) { t.has_bar(); }
        )
{
    // [...]
}

If that looks too convoluted, you can also define that as a new concept:

template<typename T>
concept has_foo_or_bar =
    requires (T t) { t.has_foo(); } ||
    requires (T t) { t.has_bar(); };

template<typename T>
bool check(const T& t)
    requires( has_foo_or_bar<T>(t) )
{
    // [...]
}

Compiler Explorer

1
  • For the record, (...) are redundant. Mar 21 at 22:45
4

You can write two distinct concepts:

#include <iostream>
#include <concepts>

struct Foo
{
    bool foo() const { return true; }
};

struct Bar
{
    bool bar() const { return true; }
};

template<typename T>
concept has_foo = requires (T t)
{ { t.foo() } -> std::same_as<bool>; };

template<typename T>
concept has_bar = requires (T t)
{ { t.bar() } -> std::same_as<bool>; };

Then, you could either add another concept:

template<typename T>
concept has_foo_or_bar = has_bar<T> or has_foo<T>;

Or use those separately:

template<typename T>
bool check(const T& t)
    requires  has_foo<T> or has_bar<T>
{
    if constexpr ( has_foo<T> )
    {
        // ...
    }
    if constexpr ( has_bar<T> )
    {
        //  ...
    }
    // ...
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.