编译时映射和逆映射值

钥匙

有人可以推荐一种更优雅的方法来实现这些编译时常量吗?

template <int> struct Map;
template <> struct Map<0> {static const int value = 4;};
template <> struct Map<1> {static const int value = 8;};
template <> struct Map<2> {static const int value = 15;};

template <int> struct MapInverse;
template <> struct MapInverse<4> {static const int value = 0;};
template <> struct MapInverse<8> {static const int value = 1;};
template <> struct MapInverse<15> {static const int value = 2;};

这些值需要在我的程序中为constexpr,但是反向映射的值更新起来变得很繁琐(而且容易出错或什至忘记做)。

沃尔特斯

使用C ++ 11进行线性搜索的另一种TMP方法:

#include <type_traits>

// === Types:
// Usage:
//    Function<Map<x1,y1>,Map<x2,y2>,...>
template<int D, int R> struct Map { enum { domain=D, range=R }; };
template<typename ...A> struct Function {};

// === Metafunctions:
// Usage:
//    ApplyFunction<x,F>::value
template<int I, typename M> struct ApplyFunction;
// Usage:
//    ApplyFunctionInverse<x,F>::value
template<int I, typename M> struct ApplyFunctionInverse;

// ==== Example:
// Define function M to the mapping in your original post.
typedef Function<Map<0,4>,Map<1,8>,Map<2,15>> M;

// ==== Implementation details
template<typename T> struct Identity { typedef T type; };
template<int I, typename A, typename ...B> struct ApplyFunction<I, Function<A,B...> > {
   typedef typename
      std::conditional <I==A::domain
                       , Identity<A>
                       , ApplyFunction<I,Function<B...>> >::type meta;
   typedef typename meta::type type;
   enum { value = type::range };
};
template<int I, typename A> struct ApplyFunction<I, Function<A>> {
   typedef typename
       std::conditional <I==A::domain
                        , Identity<A>
                        , void>::type meta;
   typedef typename meta::type type;
   enum { value = type::range };
};
// Linear search by range
template<int I, typename A> struct ApplyFunctionInverse<I, Function<A>> {
   typedef typename
       std::conditional <I==A::range
                        , Identity<A>
                        , void>::type meta;
   typedef typename meta::type type;
   enum { value = type::domain };
};
template<int I, typename A, typename ...B> struct ApplyFunctionInverse<I, Function<A,B...> > {
   typedef typename
       std::conditional <I==A::range
                        , Identity<A>
                        , ApplyFunctionInverse<I,Function<B...>> >::type meta;
   typedef typename meta::type type;
   enum { value = type::domain };
};

// ==============================
// Demonstration
#include <iostream>
int main()
{
   // Applying function M
   std::cout << ApplyFunction<0,M>::value << std::endl;
   std::cout << ApplyFunction<1,M>::value << std::endl;
   std::cout << ApplyFunction<2,M>::value << std::endl;

   // Applying function inverse M
   std::cout << ApplyFunctionInverse<4,M>::value << std::endl;
   std::cout << ApplyFunctionInverse<8,M>::value << std::endl;
   std::cout << ApplyFunctionInverse<15,M>::value << std::endl;
}

对于该应用程序,我更喜欢zch的C ++ 11解决方案,但也许有人会从这种方法中找到价值。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

飞镖逆映射

来自分类Dev

Fortran 中的逆映射

来自分类Dev

FHIR与原型之间是否可逆映射?

来自分类Dev

从Python列表到整数索引的逆映射

来自分类Dev

映射时转换值

来自分类Dev

如何映射映射的键和值?

来自分类Dev

如何持续处理涉及@JoinFormula的@OneToOne逆映射?

来自分类Dev

逆映射其中GetValue返回一个列表

来自分类Dev

对和值之间的Java映射

来自分类Dev

映射Serie和Dataframe值

来自分类Dev

异构映射,在编译时依赖类型

来自分类Dev

在C ++中创建编译时键值映射

来自分类Dev

取消引用映射指针时的编译时错误

来自分类Dev

加载异步映射时传递Null值

来自分类Dev

映射 JavaRDD 时删除空值

来自分类Dev

使用多个键和值进行映射

来自分类Dev

角度如何映射键和值

来自分类Dev

映射以保存多组键和值

来自分类Dev

Automapper 编译映射

来自分类Dev

在编译使用C ++映射的代码时遇到问题

来自分类Dev

将vim命令映射到编译时功能

来自分类Dev

基于模板参数的条件编译时类型映射

来自分类Dev

插入共享内存中的映射的映射时发生编译器错误

来自分类Dev

内核编译:生成modules.dep和映射文件

来自分类Dev

如何为某些终端映射和编译terminfo?

来自分类Dev

获取映射值中映射值的值

来自分类Dev

将对象转换为字符串Java时,对象映射器删除空值和空值

来自分类Dev

映射顶级值和嵌套对象中的单个值

来自分类Dev

NHibernate无法编译映射文件