部分模板特化 SFINAE
Partial Template Specialization SFINAE
我有以下 class 个模板:
template<bool head, bool... tail>
struct var_and {
static constexpr bool value = head && var_and<tail...>::value;
};
template<bool b> struct var_and<b> {
static constexpr bool value = b;
};
template<typename... Ts>
struct type_list {};
template <typename T, typename Enable = void>
class foo;
template <typename... T>
class foo<type_list<T...>, std::enable_if_t<var_and<std::is_integral_v<T>...>::value>> {};
当我尝试匹配专业时:
foo<type_list<int, int, int>> test{};
我得到一个错误:
Error C2079 'test' uses undefined class 'ECS::foo<ECS::type_list<int,int,int>,void>'
同时我得到这些错误:
more than one partial specialization matches the template argument list of class "ECS::foo<ECS::type_list<int, int, int>, void>"
"ECS::foo<ECS::type_list<T...>, std::enable_if_t<ECS::var_and<std::is_integral_v<T>...>::value, void>>"
"ECS::foo<ECS::type_list<T...>, std::enable_if_t<ECS::var_and<std::is_integral_v<T>...>::value, void>>"
... (The exact same error message 6 more times)
我如何专门使用 SFINAE 来强制执行可变类型包中类型的类型特征?
我毫不费力地让它为单一类型参数工作:
http://www.cppsamples.com/common-tasks/class-template-sfinae.html
我知道我可以简单地使用 static_assert,但我想知道是否也可以不用。
解决方法如下所示:
#include <type_traits>
template <bool...>
struct bool_pack { };
template <bool... Bs>
using var_and = std::is_same<bool_pack<true, Bs...>, bool_pack<Bs..., true>>;
template<typename... Ts>
struct type_list {};
template <typename T, typename Enable = void>
class foo;
template <typename... T>
class foo<type_list<T...>, std::enable_if_t<var_and<std::is_integral<T>::value...>::value>> {};
int main()
{
foo<type_list<int, int, int>> {};
}
我有以下 class 个模板:
template<bool head, bool... tail>
struct var_and {
static constexpr bool value = head && var_and<tail...>::value;
};
template<bool b> struct var_and<b> {
static constexpr bool value = b;
};
template<typename... Ts>
struct type_list {};
template <typename T, typename Enable = void>
class foo;
template <typename... T>
class foo<type_list<T...>, std::enable_if_t<var_and<std::is_integral_v<T>...>::value>> {};
当我尝试匹配专业时:
foo<type_list<int, int, int>> test{};
我得到一个错误:
Error C2079 'test' uses undefined class 'ECS::foo<ECS::type_list<int,int,int>,void>'
同时我得到这些错误:
more than one partial specialization matches the template argument list of class "ECS::foo<ECS::type_list<int, int, int>, void>"
"ECS::foo<ECS::type_list<T...>, std::enable_if_t<ECS::var_and<std::is_integral_v<T>...>::value, void>>"
"ECS::foo<ECS::type_list<T...>, std::enable_if_t<ECS::var_and<std::is_integral_v<T>...>::value, void>>"
... (The exact same error message 6 more times)
我如何专门使用 SFINAE 来强制执行可变类型包中类型的类型特征?
我毫不费力地让它为单一类型参数工作: http://www.cppsamples.com/common-tasks/class-template-sfinae.html
我知道我可以简单地使用 static_assert,但我想知道是否也可以不用。
解决方法如下所示:
#include <type_traits>
template <bool...>
struct bool_pack { };
template <bool... Bs>
using var_and = std::is_same<bool_pack<true, Bs...>, bool_pack<Bs..., true>>;
template<typename... Ts>
struct type_list {};
template <typename T, typename Enable = void>
class foo;
template <typename... T>
class foo<type_list<T...>, std::enable_if_t<var_and<std::is_integral<T>::value...>::value>> {};
int main()
{
foo<type_list<int, int, int>> {};
}