繁体   English   中英

如何使用来自 C++14 和 C++1z 的特性使这个可变参数模板代码更短?

[英]How can I make this variadic template code shorter using features from C++14 and C++1z?

这是一个代码片段,我将使用它来检查可变参数模板类型是否唯一:

template <typename...>
struct is_one_of;

template <typename F>
struct is_one_of<F> {
    static constexpr bool value = false;
};

template <typename F, typename S, typename... T>
struct is_one_of<F, S, T...> {
    static constexpr bool value =
        std::is_same<F, S>::value || is_one_of<F, T...>::value;
};

template <typename...>
struct is_unique;

template <>
struct is_unique<> {
    static constexpr bool value = true;
};

template <typename F, typename... T>
struct is_unique<F, T...> {
    static constexpr bool value =
        is_unique<T...>::value && !is_one_of<F, T...>::value;
};

int main() {
    constexpr bool b = is_unique<bool, int, double>::value;
    constexpr bool c = is_unique<int, char, int>::value;
    static_assert(b == true && c == false, "!");
}

有没有办法使用 C++14 和 C++1z 中引入的功能使这段代码更短和/或更简洁? 或者有没有更好的方法来使用新功能达到同样的效果?

在 C++1z 的情况下,我的意思是:在最新版本的 Clang 和 GCC 中已经可用的功能。

我们最近在 C++1z 草案中添加了std::disjunction ,它可用于is_one_of (并且一旦找到匹配项,它就会停止实例化,有关更多详细信息,请参阅链接):

template <typename F, typename... T>
  using is_one_of = std::disjunction<is_same<F, T>...>;

这已经在 GCC 主干中实现了。 对于旧版本的 GCC,您可以使用实现细节__or_代替:

template <typename F, typename... T>
  using is_one_of = std::__or_<is_same<F, T>...>;

或者使用 C++11 工具手动实现disjunction ,如上面链接的提案末尾所示。

#include <type_traits>

template <typename F, typename... Ts>
constexpr bool is_one_of = (std::is_same<F, Ts>{} || ...);

template <typename...>
constexpr bool is_unique = true;

template <typename F, typename... Ts>
constexpr bool is_unique<F, Ts...> = is_unique<Ts...> && !is_one_of<F, Ts...>;

演示

我(现在)建议使用 STL 函数的std::conj/disj/nega系列:

#include <type_traits>

template <typename H, typename... T>
struct is_one_of : std::disjunction<std::is_same<H, T>...> {};

template <typename H, typename... T>
struct is_unique : std::conjunction<std::negation<std::is_same<H, T>>..., is_unique<T...>> {};

template <typename H>
struct is_unique<H> : std::true_type {};

int main()
{
    static_assert(is_one_of<int, char, double, int, bool>::value);
    static_assert(is_unique<int, char, double, bool>::value);
    static_assert(!is_unique<int, int, char, double, bool>::value);
}

当为这些情况设计的fold-expressions被释放到语言中时,这将变得微不足道:

namespace stx = std::experimental;

template <typename H, typename... T>
struct is_one_of {
    static constexpr bool value = (stx::is_same_v<H, T> || ...);
};

template <typename H, typename... T>
struct is_unique {
    static constexpr bool value = (!stx::is_same_v<H, T> && ... && is_unique<T...>::value);
};

template <typename H>
struct is_unique<H> : std::true_type {};

就折叠表达式部分而言,我与 Brian Rodriguez 和 Piotr Scontnincki 的回答一致。 在折叠表达式出现之前,您可以通过删除不完整的主模板来稍微缩小现有代码,如下所示:

template <typename...>
struct is_one_of {
    static constexpr bool value = false;
};

template <typename F, typename S, typename... T>
struct is_one_of<F, S, T...> {
    static constexpr bool value =
        std::is_same<F, S>::value || is_one_of<F, T...>::value;
};

template <typename...>
struct is_unique {
    static constexpr bool value = true;
};

template <typename F, typename... T>
struct is_unique<F, T...> {
    static constexpr bool value = is_unique<T...>::value && !is_one_of<F, T...>::value;
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM