Home > Software engineering >  How to use concepts in if statement
How to use concepts in if statement

Time:02-20

I have a concept which checks whether a type is iterable or not

template<typename T>
concept Iterable = requires(T t) {
    t.begin();
};

I cannot use it in template due to problems with overloading, so I

template<typename T>
void universal_function(T x) {
    if (x is Iterable)
        // something which works with iterables
    else if (x is Printable)
       // another thing
    else
       // third thing
}

CodePudding user response:

You can use if constexpr to achieve the desired behavior:

if constexpr (Iterable<T>) {
    // ...
} else if constexpr (Printable<T>) {
    // ...
} else {
    // ...
}

CodePudding user response:

You can directly write the requires clause inside the if to determine the validity of the expression, something like this

template<typename T>
void universal_function(T x) {
    if constepxr (requires {  x.begin(); }) {
        // something which works with iterables
    }
    else if constepxr (requires {  std::cout << x; }) {
       // another thing
    }
    else {
       // third thing
    }
}

But it seems that only detecting whether x.begin() is well-formed is not enough for iterable types, the standard library already has a concept for this, namely std::ranges::range:

if constepxr (std::ranges::range<T>) {
   // something which works with iterables
}
  • Related