做一个 static_assert 模板类型是另一个模板

2021-12-13 00:00:00 templates c++ c++11 boost

我如何static_assert喜欢这个?如果不是 C++ 或 C++11 中的新特性,也许 Boost 支持它?

How do I static_assert like this? Maybe Boost supports it if not C++ or new features in C++11?

template<T>
struct foo {};

template<FooType>
struct bar {
  static_assert(FooType is indeed foo<T> for some T,"failure"); //how?
};

推荐答案

您可以按照这些思路做一些事情.给定一个可以验证类是否是类模板的实例的特征:

You could do something along these lines. Given a trait that can verify whether a class is an instantiation of a class template:

#include <type_traits>

template<typename T, template<typename> class TT>
struct is_instantiation_of : std::false_type { };

template<typename T, template<typename> class TT>
struct is_instantiation_of<TT<T>, TT> : std::true_type { };

在您的程序中按如下方式使用它:

Use it as follows in your program:

template<typename T>
struct foo {};

template<typename FooType>
struct bar {
  static_assert(is_instantiation_of<FooType, foo>::value, "failure");
};

int main()
{
    bar<int> b; // ERROR!
    bar<foo<int>> b; // OK!
}

如果需要,您可以将其概括为检测类是否是具有任意数量(类型)参数的模板的实例,如下所示:

If you want, you could generalize this to detect whether a class is an instance of a template with any number of (type) parameters, like so:

#include <type_traits>

template<template<typename...> class TT, typename T>
struct is_instantiation_of : std::false_type { };

template<template<typename...> class TT, typename... Ts>
struct is_instantiation_of<TT, TT<Ts...>> : std::true_type { };

template<typename FooType>
struct bar {
  static_assert(is_instantiation_of<foo, FooType>::value, "failure");
};

然后你会在你的程序中这样使用它:

You would then use it this way in your program:

template<typename FooType>
struct bar {
  static_assert(is_instantiation_of<foo, FooType>::value, "failure");
};

int main()
{
    bar<int> b; // ERROR!
    bar<foo<int>> b; // OK!
}

这是一个noliver>noliver.

Here is a live example.

相关文章