如何静态检查可变参数模板参数列表中是否存在类型T

如何静态检查可变参数模板参数列表中是否存在类型T

本文介绍了如何静态检查可变参数模板参数列表中是否存在类型T的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图静态检查可变参数模板参数列表中是否存在类型.但是,此模板列表实际上存在于传递单一类型的类中.答案此处显示了如何检查参数列表或参数包,但是我不确定如何测试包含可变参数模板的类.

I am trying to statically check to see if a type exists in a variadic template parameter list. However, this template list actually exists within a class that is passed a single type. The answer here shows how to check a list of parameters or a parameter pack, but I am unsure how to test a class that contains variadic templates.

例如

template <typename ...S>
class Services {};

template <typename Services>
class ServiceLocator
{
public:
      template <typename T>
      T& Resolve()
      {
           static_assert( check_t_exists_in_variadic_template_within_Services );
           return Find<T>();
      }

};

如果要使用服务中模板参数列表中不存在的类型调用Resolve,我该在static_assert中写什么以确保检查到此服务定位符的每个调用并引发编译器错误?

What could I write in this static_assert to ensure that each call to this service locator is checked and a compiler error thrown if Resolve is called with a type that does not exist in the template parameter list inside Services?

我专门追求的是类似以下内容的东西:

What I am specicially after is something along the lines of:

static_assert(is_any<T,Services::S...>::value, "T does not exist in Services::S");

推荐答案

基于François的答案,这是一个简短的版本,避免使用 std :: tuple ,并使用 std :: integral_constant (通过 true/false_type )并提供C ++ 14样式的 contains_v 别名.总体思路是一样的.

Based on François' answer, here's a shorter version that avoids usage of std::tuple and uses std::integral_constant (via true/false_type) and provides a C++14-style contains_v alias. The general idea is the same though.

template <typename T, typename... Args>
struct contains;

template <typename T>
struct contains<T> : std::false_type {};

template <typename T, typename... Args>
struct contains<T, T, Args...> : std::true_type {};

template <typename T, typename A, typename... Args>
struct contains<T, A, Args...> : contains<T, Args...> {};

template <typename T, typename... Args>
constexpr bool contains_v = contains<T, Args...>::value;

您可以像这样使用它:

static_assert(contains_v<float, float, double>,
    "failure: float not among <float, double>"); // does not trigger

static_assert(contains_v<int, float, double>,
    "failure: int not among <float, double>"); // triggers

这篇关于如何静态检查可变参数模板参数列表中是否存在类型T的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 18:37