Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to do a logical 'or' of requirements in a single concept?

I have the following case


template<typename Class>
concept has_member = requires (Class t)
{ 
    // How can I write only either of the following conditions be satisfied?
    {t.isInterface() }->std::same_as<bool>;
    // or
    {t.canInterface() }->std::same_as<bool>;
    // or
    // ... more conditions!
};

struct A {
    bool isInterface() const { return true; }
};

struct B {
    bool canInterface() const { return true; }
};

void foo(const has_member auto& A_or_B)
{
    // do something
}

int main() {
    foo(A{}); // should work
    foo(B{}); // should work
}

Like I mentioned in the comments, I would like to logically or the requirements (in a single concepts), so that the class A and B can be passed to the doSomething().

As per my knowledge, the the current concept is checking all the requirements, that means a logical and.
If I take it apart to different concepts everything works, but I would need more concepts to be written tosatify the intention.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is it possoble to combne into one? something like pseudocode

template<typename Class>
concept has_member = requires (Class t)
{ 
    {t.isInterface() }->std::same_as<bool>  ||  {t.canInterface() }->std::same_as<bool>;
    // ... 
};

>Solution :

Is it possoble to combine into one?

Yes, it is possible. You can write the conjunction of two requires as follows:

template<typename Class>
concept has_member = 
    requires (Class t) { {t.isInterface() }->std::same_as<bool>; }
    || //---> like this
    requires (Class t) { {t.canInterface() }->std::same_as<bool>;};
// ... more conditions!

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading