更新时间:2023-11-30 19:27:28
将 all_true
定义为
template< bool ...> struct bool_pack;
template< bool ... v>
using all_true = std :: is_same< bool_pack< true,v ...>,bool_pack< v ...,true>>
并将您的构造函数重写为
//检查兑换为B& ;;也可以使用事实getA()是非const
template< typename ... Args,
typename = std :: enable_if_t< all_true< std :: is_convertible< Args& {} ...> {}>
C(Args& ... args):
member {args.getA()...}
{}
恐怕如果我使用std :: is_base_of这样的谓词,我会得到
a不同的构造函数实例化设置
参数,这可能会增加编译代码大小...
enable_if_t< ;
将始终产生类型 void
(仅给出一个模板参数),因此不能 is_base_of
s故障。但是,当 Args
有不同的类型,即参数的类型是不同的,则随后将实例化不同的专门化。
如果你希望构造函数采用 N
参数,您可以使用一个更简单的方法。定义
模板< std :: size_t,typename T&
using ignore_val = T;
现在部分专门化 C
p>
//未使用的主模板
模板< size_t N,typename = std :: make_index_sequence& C类;
// Partial specialization
template< size_t N,std :: size_t ... indices>
class C< N,std :: index_sequence< indices ...>>
{/ * ... * /};
部分特化中的构造函数的定义现在变得不重要
C(ignore_val< indices,B&> ... args):
member {args.getA()...}
{}
此外,您不必担心大量专业化。
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://***.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>
.
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()...}
{}
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_of
s 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.