std :: is_convertible的变体版本? [英] Variadic version of std::is_convertible?
问题描述
是否可以编写 std :: is_convertible
的可变版本?例如 are_convertible< T1,T2,T3,T4>
将返回 is_convertible< T1,T3& &&& is_convertible< T2,T4>
。我一直在想这个几个小时,但不能提出任何合理的。
Is it possible to write a variadic version of std::is_convertible
? For example are_convertible<T1, T2, T3, T4>
would return is_convertible<T1, T3> && is_convertible<T2, T4>
. I've been thinking about this for a few hours but couldn't come up with anything reasonable.
要澄清我想使用它有点像这样:
To clarify I want to use it somewhat like this:
template <class ...Args1>
struct thing
{
template <class ...Args2>
enable_if_t<are_convertible<Args2..., Args1...>::value>
foo(Args2 &&...args){}
}
推荐答案
您不需要连接 Args2 ...
和 Args1 ...
,你不应该,因为这样做使得不可能告诉 Args2 ...
结束和 Args1 .. 。
开始。以允许单独提取多个可变参数的方式传递多个可变参数的方式是将它们包装在另一个模板中:给定一个可变参数模板 my_list
,您可以将 my_convertible
称为
You don't need to concatenate Args2...
and Args1...
, and you shouldn't, since doing to makes it makes it impossible to tell where Args2...
ends and Args1...
begins. The way to pass multiple variadic arguments in a way that allows them to be extracted individually is wrapping them in yet another template: given a variadic template my_list
, you could structure your my_convertible
to be called as
my_convertible<my_list<Args2...>, my_list<Args1...>>
标准库已经有一个可变参数模板在这里工作: tuple
。不仅如此, tuple< Args2 ...>
可转换为 tuple< Args1 ...>
当且仅当 Args2 ...
可以转换为 Args1 ...
,所以你可以写: p>
The standard library already has a variadic template that works well here: tuple
. Not only that, but tuple<Args2...>
is convertible to tuple<Args1...>
if and only if Args2...
are convertible to Args1...
, so you can just write:
std::is_convertible<std::tuple<Args2...>, std::tuple<Args1...>>
这篇关于std :: is_convertible的变体版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!