C++ 布尔技巧和模板参数
考虑bool技巧来检查一组类型是否都是相同的类型:C++ 布尔技巧和模板参数,c++,templates,c++14,variadic-templates,template-templates,C++,Templates,C++14,Variadic Templates,Template Templates,考虑bool技巧来检查一组类型是否都是相同的类型: template<typename Type, typename... Types> static constexpr bool allOf = std::is_same< std::integer_sequence<bool, true, std::is_same<Type, Types>::value...>, std::integer_sequence<bool, std::
template<typename Type, typename... Types>
static constexpr bool allOf = std::is_same<
std::integer_sequence<bool, true, std::is_same<Type, Types>::value...>,
std::integer_sequence<bool, std::is_same<Type, Types>::value..., true>
>::value;
有没有办法将它与给定模板参数的专门化一起使用?例如,使用以下代码:
template<typename> struct S {};
template<typename... Args>
void f(Args&&... args) {
static_assert(allOf<S, Args...>, "!");
// ...
}
我想检查Types
中的每个T
都是S
形式的特殊化,不管U
是什么
有可能吗?下面是什么
template <typename>
struct S
{ };
template <template <typename> class, typename>
struct isS
{ static constexpr bool value { false } ; };
template <template <typename> class S, typename U>
struct isS<S, S<U>>
{ static constexpr bool value { true } ; };
template<template<typename> class Type, typename... Types>
static constexpr bool allOf = std::is_same<
std::integer_sequence<bool, true, isS<Type, Types>::value...>,
std::integer_sequence<bool, isS<Type, Types>::value..., true>
>::value;
模板
结构
{ };
模板
结构isS
{static constexpr bool value{false};};
模板
结构isS
{static constexpr bool value{true};};
模板
静态constexpr bool allOf=std::是否相同<
std::整数_序列,
std::整数_序列
>::价值;
我们只需要检查专业化:
template <class T, template <class...> class Z>
struct isZ : std::false_type { };
template <class... Args, template <class...> class Z>
struct isZ<Z<Args...>, Z> : std::true_type { };
因此:
static_assert(allOf<isZ<decay_t<Args>, S>::value...>::value, "!");
static_断言(allOf::value,“!”);
那个bool把戏很好,谁想出了这个?我不会改变bool把戏,但叫它不同的名字:@skypjack啊,我明白了。我认为这是因为非专业化不处理非模板类。修正:@Johanneschaub litb我已经习惯了这部电影,这是我第一次看到它的地方。@Quentin谢谢。我没能找到它。不管怎么说,我看到的那个有点不同,但我同意信任哥伦布。:-)看起来不错。我不知道为什么我没有想到。我不应该在晚上提出问题,然后在第二天早上立即对问题三思而后行-D+1因为这是一个有效的解决方案,让我们看看在接受它之前是否出现了其他问题。@skypjack-你没有想到它只是因为现在是午夜;午夜时分,我正处于最佳状态:-)有一个可能的解释:你没有儿子。是吗-D@skypjack-没有,没有儿子;也许我是吸血鬼;-)
template <class T, template <class...> class Z>
struct isZ : std::false_type { };
template <class... Args, template <class...> class Z>
struct isZ<Z<Args...>, Z> : std::true_type { };
template <bool... bs>
using allOf = std::is_same<
std::integer_sequence<bool, true, bs...>,
std::integer_sequence<bool, bs..., true>>;
static_assert(allOf<isZ<decay_t<Args>, S>::value...>::value, "!");