实现可变类型性状 [英] Implementing variadic type traits
问题描述
我正在寻找模式将C ++类型特征转换为可变对应。使用方法来处理问题是非常理想的,生成式编程模式可以自动完成任务。
I'm looking for a pattern to convert C++ type traits into their variadic counterparts. A methodology to approach the problem would be appreciated and generative programming patterns to automate the task would be ideal.
采取以下措施:
std::is_same<T, U>::value;
我想写一个像这样的trait:
I want to write a trait that works like so :
std::are_same<T1, T2, T3, T4>::value;
目前方法
这是很好的 直接 来实现 are_same
; 寻求一般解决方案,我们可以为实施通用量化的任何变化性状提供一个工具:
Current approach
It's pretty straightforward to implement the are_same
; Seeking a general solution we can come up with a tool for any variadic trait implementing universal quantification :
template<template<class,class> class F, typename...Ts>
struct Univ;
template<template<class, class> class F, typename T, typename U, typename...Ts>
struct Univ<F, T, U, Ts...>
{
static const int value = F<T, U>::value && Univ<F, U, Ts...>::value;
};
template<template<class, class> class F, typename T>
struct Univ<F, T>
{
static const int value = 1;
};
,因此 are_same
可以 写为
so that eg are_same
could be written as
Univ<is_same,int, int, int>::value
如 are_classes
, are_scalars
等
小调整可以提供前面代码段中的存在性量化(将&&
替换为 ||
),以便我们以下列方式创建 exists_same
的特征:
Minor tweaks could give existential quantification out of the previous snippet (replacing &&
with ||
) so that we create traits like exist_same
in the following fashion :
Exist<is_same, int, double, float>::value
问题
之前的封面概述 type traits
- 复合类型类别
- 输入属性
- 支持的操作
- Primary type categories
- Composite type categories
- Type properties
- Supported operations
>如何推广类型特征,如下所示:
enable_if -> enable_if_any // enable if any clause is true
enable_if_all // enalbe if all clauses are true
enable_for // enable only for the type provided
上面的 exists_same
示例过于简单。有正确实现的任何想法?
The exist_same
example above is oversimplified. Any ideas for a correct implementation?
有type_traits 返回修改类型。任何建议缩放到任意数量的类型的实现?
There are type_traits that "return" modified types. Any suggestion for scaling those to implementations for arbitrary number of types ?
是否有type_traits ,这些不能扩展到任意数量的类型参数?
推荐答案
我不完全明白你想要达到什么,但以下帮助可能是有用的, bool_sequence
:
I don't fully understand what exactly you'd like to achieve, but the following helpers might be useful, starting with bool_sequence
:
#include <type_traits>
// Note: std::integer_sequence is C++14,
// but it's easy to use your own version (even stripped down)
// for the following purpose:
template< bool... Bs >
using bool_sequence = std::integer_sequence< bool, Bs... >;
// Alternatively, not using C++14:
template< bool... > struct bool_sequence {};
接下来,您可以检查是否所有或任何布尔值或设置与:
next, you can check if all or any boolean value or set with these:
template< bool... Bs >
using bool_and = std::is_same< bool_sequence< Bs... >,
bool_sequence< ( Bs || true )... > >;
template< bool... Bs >
using bool_or = std::integral_constant< bool, !bool_and< !Bs... >::value >;
它们派上用场,作为更高级和专门特性的构建块。例如,你可以这样使用它们:
they come in handy as building blocks for more advanced and specialized traits. For example, you could use them like this:
typename< typename R, bool... Bs > // note: R first, no default :(
using enable_if_any = std::enable_if< bool_or< Bs... >::value, R >;
typename< typename R, bool... Bs > // note: R first, no default :(
using enable_if_all = std::enable_if< bool_and< Bs... >::value, R >;
typename< typename T, typename... Ts >
using are_same = bool_and< std::is_same< T, Ts >::value... >;
这篇关于实现可变类型性状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!