比较可变模板
Comparing variadic templates
本文关键字:比较 更新时间:2023-10-16
如果我有两个可变模板参数,A
和B
,我如何在编译时确保A
的所有成员的类型也是B
的子集的类型(以相同的顺序)?
的例子:
template<typename...A>
struct Foo {
template<typename...B>
static void bar()
{
}
}
...
Foo<Apple, Orange>:: template bar<Apple, Orange, Grape>(); // this compiles
Foo<Apple, Orange>:: template bar<Orange, Grape>(); // this doesn't
对于一般子集,我不知道,但如果您可以保证B
是A..., More...
的形式,那么这可能会做:
#include <functional>
#include <tuple>
template <typename ...A>
struct var_equal : std::false_type { };
template <typename A1, typename ...Aother, typename B1, typename ...Bother>
struct var_equal<std::tuple<A1, Aother...>, std::tuple<B1, Bother...>>
{
static const bool value = std::is_same<A1, B1>::value && var_equal<std::tuple<Aother...>, std::tuple<Bother...>>::value;
};
template <typename ...B>
struct var_equal<std::tuple<>, std::tuple<B...>> : std::true_type { };
template<typename...A>
struct Foo {
template<typename...B>
static void bar()
{
static_assert(var_equal<std::tuple<A...>, std::tuple<B...>>::value, "Hello");
}
};
(对不起,var_equal
是个糟糕的名字。它应该叫一个更合适的名字,比如initial_equal
)
更新:这是一般的解决方案,由Luc Danton详细制定(见这里他的漂亮风格的代码):
#include <type_traits>
#include <tuple>
template <typename Sub, typename Super>
struct subset_of : std::false_type {};
template<typename Same, typename... AOther, typename... BOther>
struct subset_of<std::tuple<Same, AOther...>, std::tuple<Same, BOther...>>
: subset_of<
std::tuple<AOther...>
, std::tuple<BOther...>
> {};
template<typename ADifferent, typename BDifferent, typename... AOther, typename... BOther>
struct subset_of<std::tuple<ADifferent, AOther...>, std::tuple<BDifferent, BOther...>>
: subset_of<
std::tuple<ADifferent, AOther...>
, std::tuple<BOther...>
> {};
template<typename... B>
struct subset_of<std::tuple<>, std::tuple<B...>>: std::true_type {};
template<typename... A>
struct Foo {
template<typename... B>
static void bar()
{
static_assert(subset_of<std::tuple<A...>, std::tuple<B...>>::value, "Hello");
}
};
测试用例:struct Apple{}; struct Orange{}; struct Grape{};
int main()
{
Foo<Apple, Orange>::bar<Apple, Orange, Grape>(); // this compiles
Foo<Apple, Orange>::bar<Grape, Apple, Grape, Orange, Grape>(); // this also compiles
Foo<Apple, Orange>::bar<Orange, Grape>(); // this doesn't
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么比较运算符如此快速
- 我可以使用 g++ 进行三种比较 (<=>) 吗?
- 比较字符数组
- 将模板化的类型与C++中的某些类/类型进行比较
- C++自定义比较函数
- 如何比较自定义类的std::变体
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- std::设置自定义比较器
- 布尔比较运算符是如何在C++中工作的
- C++将目录中的所有文件与::filesystem进行比较
- shell排序中的交换和比较
- 如何在C++中比较两个char数组
- catch框架有没有办法比较流或文件
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 智能指针作为无序映射键,并通过引用进行比较
- 比较if语句中的数组值和int值
- 对于循环变体比较
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 比较两个大小不等的映射c++