做一个static_assert一个模板类型的另一个模板 [英] Doing a static_assert that a template type is another template

查看:206
本文介绍了做一个static_assert一个模板类型的另一个模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何 static_assert 这样吗?也许加速支持,如果没有C ++或新功能,在C ++ 11?

 模板< T>
结构foo的{};模板< FooType>
结构bar {
  static_assert(FooType确实是富< T>对于一些T,失败); //怎么样?
};


解决方案

您可以做的东西沿着这些路线。给定一个特征,可以验证类是否是一个类模板的一个实例:

 的#include< type_traits>模板< typename的T,模板< typename的>一流的TT>
结构is_instantiation_of:性病:: false_type {};模板< typename的T,模板< typename的>一流的TT>
结构is_instantiation_of< TT< T&GT ;, TT> :性病:: true_type {};

在你的程序如下使用它:

 模板< typename的T>
结构foo的{};模板< typename的FooType>
结构bar {
  static_assert(is_instantiation_of< FooType,富> ::价值,失败);
};诠释的main()
{
    巴≤; INT> b: //错误!
    巴≤;富< INT>> b: // 好!
}

如果你愿意,你可以概括这个检测类是否是一个模板与任意数量的(类型)的参数,像这样一个实例:

 的#include< type_traits>模板<模板< typename的...>类TT,类型名T>
结构is_instantiation_of:性病:: false_type {};模板<模板< typename的...>类TT,类型名称... TS>
结构is_instantiation_of< TT,TT< TS ...>> :性病:: true_type {};模板< typename的FooType>
结构bar {
  static_assert(is_instantiation_of<富,FooType> ::价值,失败);
};

您会然后用它在你的程序是这样的:

 模板< typename的FooType>
结构bar {
  static_assert(is_instantiation_of<富,FooType> ::价值,失败);
};诠释的main()
{
    巴≤; INT> b: //错误!
    巴≤;富< INT>> b: // 好!
}

下面是一个<一个href=\"http://coliru.stacked-crooked.com/view?id=bd92515881e87149e3ca436c493d8609-a63ad9fea3739cb794a74965e5d28fb3\">live例如

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!
}

Here is a live example.

这篇关于做一个static_assert一个模板类型的另一个模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆