C++17:泛型(基于多重继承?)检查参数包中的模板

标签 c++ templates c++17 metaprogramming

我需要一些代码来检查某个模板是否是参数包的一部分。为了实现对普通类的检查,我使用了概述的基于多重继承的方法,例如通过 Louis Dionne hereAgustín Bergé here .

类测试

想法是包装每个类 TPackEntry 的包装中类,然后有 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> classtemplate<std::size_t, typename> classtemplate <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/

相关文章:

c++ - Visual Studio 中的 pthread_create 错误

c++ - 使用类 vector 时会遇到内存泄漏吗? (C++)

c++ - 无法推断模板参数?

c++ - 成员模板的别名模板

C++删除文件中的文本行

c++ - 重载 Iostream C++

c++ - 模板;构造函数;编译时间

templates - 加载动态 HTML 以显示自定义模板

c++ - 可变参数模板类中的模板特化和选择

c++ - period 必须是 C++17 chrono 库中 ratio 的特化吗?