Update: conditional explicit has made it into the C++20 draft. more on cppreference
The cppreference std::tuple constructor page has a bunch of C++17 notes saying things like:
This constructor is
explicit
if and only ifstd::is_convertible<const Ti&, Ti>::value
is false for at least onei
How can one write a constructor that is conditionally explicit? The first possibility that came to mind was explicit(true)
but that's not legal syntax.
An attempt with enable_if
was unsuccessful:
// constructor is explicit if T is not integral
struct S {
template <typename T,
typename = typename std::enable_if<std::is_integral<T>::value>::type>
S(T) {}
template <typename T,
typename = typename std::enable_if<!std::is_integral<T>::value>::type>
explicit S(T) {}
};
with the error:
error: ‘template<class T, class> S::S(T)’ cannot be overloaded
explicit S(T t) {}
See Question&Answers more detail:os