Steakfly Steakfly - 11 days ago 7
C++ Question

Check traits for all variadic template arguments

Background : I've created the following class

C
, whose constructor should take
N
variables of type
B&
:

class A;
class B
{
A* getA();
};

template<size_t N>
class C
{
public:
template<typename... Args>
inline C(Args&... args) :
member{args.getA()...}
{}
private:
std::array<A*, N> member;
};


Problem : my problem is how to constraint the variadic
Args
to be all of type
B
?

My partial solution : I wanted to define a predicate like :

template <typename T, size_t N, typename... Args>
struct is_range_of :
std::true_type // if Args is N copies of T
std::false_type // otherwise
{};


And redefine my constructor accordingly :

template <typename... Args,
typename = typename std::enable_if<is_range_of_<B, N, Args...>::value>::type
>
inline C(Args&... args);


I've seen a possible solution on this post : http://stackoverflow.com/a/11414631, which defines a generic
check_all
predicate :

template <template<typename> class Trait, typename... Args>
struct check_all :
std::false_type
{};

template <template<typename> class Trait>
struct check_all<Trait> :
std::true_type
{};

template <template<typename> class Trait, typename T, typename... Args>
struct check_all<Trait, T, Args...> :
std::integral_constant<bool, Trait<T>::value && check_all<Trait, Args...>::value>
{};


So, I could write something like :

template <typename T, size_t N, typename... Args>
struct is_range_of :
std::integral_constant<bool,
sizeof...(Args) == N &&
check_all<Trait, Args...>::value
>
{};


Question 1 : I don't know how to define the
Trait
, because I need somehow to bind
std::is_same
with
B
as first argument. Is there any means of using the generic
check_all
in my case, or is the current grammar of C++ incompatible ?

Question 2 : My constructor should also accept derived classes of
B
(through a reference to
B
), is it a problem for template argument deduction ? 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...

Edit : For example, I have
B1
and
B2
that inherits from
B
, I call
C<2>(b1, b1)
and
C<2>(b1, b2)
in my code, will it create two instances (of
C<2>::C<B1, B1>
and
C<2>::C<B1, B2>
) ? I want only instances of
C<2>::C<B, B>
.

Answer

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.