Check traits for all variadic template arguments

Solution 1:

Define all_true as

template <bool...> struct bool_pack;

template <bool... v>
using all_true = std::is_same<bool_pack<true, v...>, bool_pack<v..., true>>;

And rewrite your constructor to

// Check convertibility to B&; also, use the fact that getA() is non-const
template<typename... Args,
       typename = std::enable_if_t<all_true<std::is_convertible<Args&, B&>{}...>>
C(Args&... args) :
    member{args.getA()...}
{}

Alternatively, under C++17,

template<typename... Args,
       typename = std::enable_if_t<(std::is_convertible_v<Args&, B&> && ...)>>
C(Args&... args) :
    member{args.getA()...}
{}

I am afraid that if I use a predicate like std::is_base_of, I will get a different instantiation of the constructor for each set of parameters, which could increase compiled code size...

enable_if_t<…> will always yield the type void (with only one template argument given), so this cannot be is_base_ofs fault. However, when Args has different types, i.e. the types of the arguments are distinct, then subsequently different specializations will be instantiated. I would expect a compiler to optimize here though.


If you want the constructor to take precisely N arguments, you can use a somewhat easier method. Define

template <std::size_t, typename T>
using ignore_val = T;

And now partially specialize C as

// Unused primary template
template <size_t N, typename=std::make_index_sequence<N>> class C;
// Partial specialization
template <size_t N, std::size_t... indices>
class C<N, std::index_sequence<indices...>>
{ /* … */ };

The definition of the constructor inside the partial specialization now becomes trivial

C(ignore_val<indices, B&>... args) :
    member{args.getA()...}
{}

Also, you do not have to worry about a ton of specializations anymore.

Solution 2:

namespace detail {
    template <bool...> struct bool_pack;
    template <bool... v>
    using all_true = std::is_same<bool_pack<true, v...>, bool_pack<v..., true>>;
    template<class X> constexpr X implicit_cast(std::enable_if_t<true, X> x) {return x;}
};

The implicit_cast is also in Boost, the bool_pack stolen from Columbo.

// Only callable with static argument-types `B&`, uses SFINAE
template<typename... ARGS, typename = std::enable_if_t<
    detail::all_true<std::is_same<B, ARGS>...>>>
C(ARGS&... args) noexcept : member{args.getA()...} {}

Option one, if it's implicitly convertible that's good enough

template<typename... ARGS, typename = std::enable_if_t<
    detail::all_true<!std::is_same<
    decltype(detail::implicit_cast<B&>(std::declval<ARGS&>())), ARGS&>...>>
C(ARGS&... args) noexcept(noexcept(implicit_cast<B&>(args)...))
    : C(implicit_cast<B&>(args)...) {}

Option two, only if they are publicly derived from B and unambiguously convertible:

// Otherwise, convert to base and delegate
template<typename... ARGS, typename = decltype(
    detail::implicit_cast<B*>(std::declval<ARGS*>())..., void())>
C(ARGS&... args) noexcept : C(implicit_cast<B&>(args)...) {}

The unnamed ctor-template-argument-type is void in any successful substitution.