...

/

With if constexpr

With if constexpr

This section discusses the use of enable_if in pre C++17 versions and the use of if_constexpr in C++ 17.

Before C++17

Using enable_if

In the previous solution (pre C++17) std::enable_if had to be used:

Press + to interact
template <typename Concrete, typename... Ts>
enable_if_t<is_constructible<Concrete, Ts...>::value, unique_ptr<Concrete>>
constructArgsOld(Ts&&... params)
{
return std::make_unique<Concrete>(forward<Ts>(params)...);
}
template <typename Concrete, typename... Ts>
enable_if_t<!is_constructible<Concrete, Ts...>::value, unique_ptr<Concrete> >
constructArgsOld(...)
{
return nullptr;
}

std::is_constructible - allows us to test if a list of arguments could be used to create a given type.

Just a quick reminder about enable_if ...