我需要一些代码来检查某个模板是否是参数包的一部分。为了实现对普通类的检查,我使用了概述的基于多重继承的方法,例如通过 Louis Dionne here或 Agustín Bergé here .
类测试
想法是包装每个类 T
在 PackEntry
的包装中类,然后有 PackIndex
继承自所有PackEntry
类。这样,如果您正在寻找类(class) A
,您需要做的就是检查是否有 PackIndex
可以转换为正确的PackEntry
.把所有东西放在一起,它看起来像这样:
#include <cstddef>
#include <utility>
template <class T>
struct PackEntry
{
using type = T;
};
template <class... Ts>
struct PackIndex : PackEntry<Ts>...
{};
template <class T, class... Ts>
struct PackSearcher
{
static constexpr std::false_type check(...);
// This overload is used if the PackIndex below
// derives from PackEntry<T>, the overload above
// otherwise.
static constexpr std::true_type check(PackEntry<T>);
using type =
decltype(check(PackIndex<Ts...>{}));
static constexpr bool
value()
{
return type{};
}
};
template <class... Ts>
struct Pack
{
template<class T>
static constexpr bool has() {
return PackSearcher<T, Ts...>::value();
}
};
int main() {
static_assert(Pack<int, void>::has<int>());
static_assert(! Pack<int, void>::has<bool>());
}
测试特定模板
现在,这一切都很好,但是假设我有一个模板 Tmpl
,并且我想知道我的包是否包含该模板的任何特化。如果模板有特定的形式,我很容易做到,比如说 template <std::size_t> class Tmpl {};
基本上,唯一的区别是在 PackSearcher
里面(我现在称之为 TmplPackSearcher
,您让编译器为模板派生出正确的 N
模板参数。
#include <cstddef>
#include <utility>
template <class T>
struct PackEntry
{
using type = T;
};
template <class... Ts>
struct PackIndex : PackEntry<Ts>...
{
};
template <template <std::size_t> class T, class... Ts>
struct TmplPackSearcher
{
static constexpr std::false_type check(...);
template <std::size_t N>
static constexpr std::true_type check(PackEntry<T<N>>);
using type =
decltype(check(PackIndex<Ts...>{}));
static constexpr bool
value()
{
return type{};
}
};
template <class... Ts>
struct Pack
{
template<template <std::size_t> class T>
static constexpr bool has_tmpl() {
return TmplPackSearcher<T, Ts...>::value();
}
};
template<std::size_t I>
class Tmpl {};
int main() {
static_assert(Pack<Tmpl<1>, int>::has_tmpl<Tmpl>());
static_assert(!Pack<int>::has_tmpl<Tmpl>());
}
问题
现在,我的问题是:如何在不假设模板看起来像什么的情况下测试参数包中是否存在模板?即,我不想为 template<std::size_t>
编写单独的代码, template<std::size_t, typename>
, template <typename, typename>
等
如果可以使用相同的多重继承技巧完成,则加分。
最佳答案
正如@MaxLanghof 在评论中提到的,甚至不可能声明 has_tmpl
接受任意种类的模板。可能会重载 has_tmpl
具有不同的模板参数( template<std::size_t> class
、 template<std::size_t, typename> class
、 template <typename, typename> class
等),但需要无限数量的重载。
相反,让我们使用一个包装模板的类型,并公开我们需要的一切。 AFAIK 最简单的方法是(ab)使用 lambda:[]<std::size_t I>(type_identity<Tmpl<I>>){}
.
然后问题几乎是微不足道的:has_tmpl
可以简单定义为返回(std::is_invocable_v<Lambda,type_identity<Ts>> || ...)
.
完整示例:
#include <type_traits>
template<class> struct type_identity {};
template <class... Ts>
struct Pack
{
template<class Lambda>
static constexpr bool has_tmpl(Lambda) {
return (std::is_invocable_v<Lambda, type_identity<Ts>> || ...);
}
};
template<std::size_t I>
class Tmpl {};
int main() {
static_assert(Pack<Tmpl<1>, int>::has_tmpl([]<std::size_t I>(type_identity<Tmpl<I>>){}));
static_assert(!Pack<int>::has_tmpl([]<std::size_t I>(type_identity<Tmpl<I>>){}));
}
请注意,这使用了在 C++20 中标准化的 GNU 扩展(通用 lambda 的模板参数列表)。我认为这是无法避免的。
应该可以使用多重继承,但是折叠表达式要短得多;)
关于C++17:泛型(基于多重继承?)检查参数包中的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56665977/