Christophe Fuzier
Christophe Fuzier

Reputation: 759

Why is there no std equivalent of boost::enable_if?

In C++11, std::enable_if was added to the Standard Library. It is equivalent to boost::enable_if_c whose condition is a bool. This is suitable for rather simple conditions, but as soon as you use predicates that hold their result in a value constant, you have to use the more verbose construct my_predicate<MyArgs>::value to turn it into bool. This is exactly what boost::enable_if (without _c suffix) was made for.

Why is there no equivalent in Standard Library?

Upvotes: 3

Views: 459

Answers (1)

Andr&#233; Bergner
Andr&#233; Bergner

Reputation: 1439

The standard library goes a different route here. C++17 added variable templates shortcuts for all the type traits that return a ::value. The pattern is always

template <typename... Args>
some_trait_v = some_trait<Args...>::value;

For instance you can write

std::enable_if<std::is_same_v<T1,T2>>

Further the argument for enable_if could be the result of constexpr expressions, for instance

std::enable_if<some_constexpr_function<T1,T2>()>

This way is more generic and does not depend on passing something that must have a value member.

Upvotes: 5

Related Questions