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 : https://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>
.
B
, or just implicitly convertible toB
? – AdaB
, see my edit. I need a template generalization toN
arguments of a class that defines the constructorC(B& b)
. – FatefulB
is a both a stronger and a weaker constraint than being convertible. As neither implies the other. – Ada