In answering this question about trying to construct a variadic forwarding reference constructor that should only be called if no other constructor is valid. That is, if there was a:
C(const char*, size_t) { } // 1
template <typename... T, ???> C(T&&... ) { } // 2
We'd want C c1{"abc", 2};
to call (1), despite the required conversion, but C c2{1, 2, 3};
to call (2), as (1) cannot apply.
I proposed the following solution:
template <typename... T,
typename = std::enable_if_t<!std::is_constructible<C, T&&...>::value>
>
C(T&&... ) { }
And by proposed, I mean, I tried it and was surprised to discover that it actually works. It compiles and does exactly what I had hoped for on both gcc and clang. However, I am at a loss to explain why it works or even if it's actually supposed to work and gcc and clang are both just being particularly accommodating. Is it? Why?
See Question&Answers more detail:os