samedi 31 janvier 2015

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://ift.tt/1uMxNKa, 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...


Aucun commentaire:

Enregistrer un commentaire