似乎模板参数在该上下文中不可推导。
您可能想尝试使用函数参数,似乎演绎规则更容易处理。
类似于这样,如果需要将输出作为类型,可以将其包装在结构上:
#include <type_traits>
template<int x, int y, bool visible>
struct Point { };
template <typename T>
struct NoPoint { };
template <typename T>
constexpr auto Match(T &&)
{
return false;
}
template<template < auto ... > typename Head, auto ... Values>
constexpr auto Match(Head<Values...> &&)
{
return true;
}
static_assert(Match(Point<1, 2, true>{}), "I am a wombat.");
static_assert(Match(NoPoint<int>{}), "I am also a wombat.");
https://godbolt.org/g/fjofbx
目前,上述内容仅限于具有默认构造函数的类型。
你们有一些测试类型吗?
编辑:
一个改进的版本可能是,它不要求类型是默认可构造的:
#include <type_traits>
#include <utility>
template<int x, int y, bool visible>
struct Point { };
template <typename T>
struct NoPoint { };
template <typename T>
constexpr auto Match(T) -> std::false_type;
template<template < auto ... > typename Head, auto ... Values>
constexpr auto Match(Head<Values...>) -> std::true_type;
template <typename T>
struct Predicate : decltype(Match(std::declval<T>())) {};
static_assert(Predicate<Point<1, 2, true>>::value, "I am a wombat.");
static_assert(Predicate<NoPoint<int>>::value, "I am not a wombat.");
https://godbolt.org/g/iru2mZ
您还可以添加类似的内容以使其更可用:
template <typename T>
constexpr bool HasValuePack = Predicate<T>::value;
编辑
我意识到,我应该把这个包含C++变量的模板17变量,这就是定义:
//Predicate
template <typename T>
constexpr bool Predicate_v = false;
template<template < auto ... > typename Head, auto ... Values>
constexpr bool Predicate_v<Head<Values...>> = true;
这些是测试用例
//Test types
template<int x, int y, bool visible>
struct Point { };
template <typename T>
struct NoPoint { };
//Test Cases
static_assert(Predicate_v<Point<1, 2, true>>, "I am a wombat.");
static_assert(Predicate_v<NoPoint<int>>, "I am not a wombat.");
https://godbolt.org/g/c5gyhH