Conjunctions and Disjunctions

By , last updated July 15, 2019

A conjunction is a word used to connect clauses or sentences or to coordinate words in the same clause.

With the English language, the words for, and, nor, but, or, yet and so are conjunctions. The sentence “They do not gamble or smoke, for they are ascetics” is an example of a conjunction from Wikipedia.

With C++ Concepts, a conjunction is the use of two or more concepts to define requirements in one concept. It’s is essential when using many smaller constraints to build a bigger and more complete constraints.

If we have two different requirements, they are a conjunction required for a concept.

struct foo
{
    int a(){}
};

struct bar
{
    int b(){}
};

We define two structures foo and bar, with a method int a() and a method int b() each.

template<typename T>
concept bool have_a()
{
    return requires(T t)
    {
        { t.a() } -> int;
    };
}

template<typename T>
concept bool have_b()
{
    return requires(T t)
    {
        { t.b() } -> int;
    };
}

Then we define one concept for each, one that requires the method int a() and one that requires the method int b().

template<typename T>
concept bool have_ab() 
{
    return have_a<T>() && have_b<T>();
}

The concept have_ab() returns true only if the type T has both methods int a() and int b() available and callable.

struct foobar : foo, bar
{};

One such struct is foobar, which inherits from both foo and bar.

Disjunctions

Disjunctions are similar to the or-operator, ||, in regular C++.

A disjunction is built with a left side and a right side, and is evaluated left-to-right. If the left part evaluates to true, the right part is not evaluated. It’s called evaluation short-circuiting.

template<typename T>
concept bool D1 = false;

template<typename T>
concept bool D2 = true;

template<typename T>
concept bool Disjunction = D1<T> || D2<T>;

The concept Disjunction is a disjunction. One or both sides of the concept requires clause, D1<T> || D2<T>, can be true. It only fails if both constraints fail.

The usages are not clear, but disjunctions can be used to relax the requirements in different build settings. Implementing such a system is left as an exercise to the reader.

Professional Software Developer, doing mostly C++. Connect with Kent on Twitter.