c++dictionarytemplatesinverse

Compile-time map and inverse map values


Can someone recommend a more elegant way to achieve these compile-time constants?

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

The values need to be constexpr in my program, yet the inverse mapped values are getting tedious to update (and easy to make mistakes or forget to do even).


Solution

  • Another TMP approach for a linear search using C++11:

    #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;
    }
    

    I prefer zch's C++11 solution for this application, but maybe someone will find value in this approach.