c++ - 如何将可变参数模板参数的特征值减少为一个值?

标签 c++ stl c++14 traits c++17

在试验可变参数模板时,我发现通过某种操作将特征值减少到一个最终值会很有用。我的用例是:

constexpr bool and(bool lhs, bool rhs){return lhs && rhs;}

struct Foo
{
    template<
        typename ...Ts>
    Foo(
        Ts&&... args)
            noexcept(TraitReduction<std::is_nothrow_move_constructible, and, Ts...>::value)
    {/*...*/}
}

问题是 STL 特征都是单一模板类型的。 我目前的工作解决方案是:

template<
    template<typename> class TraitT,
    bool (*Operator)(bool,bool),
    typename T1,
    typename ...Ts>
struct TraitReduction
{
    static bool const value = TraitT<T1>::value;
};

template<
    template<typename> class TraitT,
    bool (*Operator)(bool,bool),
    typename T1,
    typename T2,
    typename ...Ts>
struct TraitReduction<
    TraitT,
    Operator,
    T1,
    T2,
    Ts...>
{
    static bool const value = (*Operator)(
        TraitT<T1>::value,
        TraitReduction<TraitT, Operator, T2, Ts...>::value);
};

我的问题是,STL 是否为该任务提供了一些标准化(可能更方便)的解决方案?当然,我很乐意在这里对我当前的解决方案发表一些评论,哪些是不好的,哪些可以更好。

最佳答案

您的解决方案在实例化方面是线性的(并且没有短路的优势)

你可以在更少的实例化中做到这一点(并且仍然没有短路)

template <bool...> struct bools{};

template <template <typename> Trait, typename ... Ts>
struct all_of : std::is_same<bools<true, Trait<Ts>::value...>,
                             bools<Trait<Ts>::value..., true>> {};

您可以使用 std::conjunction,它是线性的,但有短路。

c++17 和折叠表达式有很好的语法,实例化较少(但没有短路(用于实例化)):

(Trait<Ts>::value && ...)

关于c++ - 如何将可变参数模板参数的特征值减少为一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40536391/

相关文章:

c++ - 在运行时删除/修改静态 Qt 资源

c++ - 应该 std::auto_ptr<>::operator = 重置/取消分配其现有指针?

c++ - C++中的基本问题

c++ - 具有模板参数的通用 lambda 函数

c++ - 为什么在析构函数中将虚拟表设置回该级别?

C++ : CRTP destructor?

c++ - STL映射简单插入内存问题

c++ - 我们可以删除通过引用传递的对象吗?

c++ - `pair::operator=(pair&&)` 错误与 `auto&` 推断 move 操作 - libstdc++ 回归?

c++ - 是否允许元组的元组?