Alias template specialisation

后端 未结 4 1428

Can alias templates (14.5.7) be explicitly specialised (14.7.3)?

My standard-fu fails me, and I can\'t find a compiler to test on.

The text \"when a template

4条回答
  •  盖世英雄少女心
    2020-11-27 17:59

    If you need pointwise mapping from something to types, this works(in gcc 4.8.3):

    //  int to type mapper
    template
    struct BitsToTypesMap
    {
        typedef void TYPE;  //  default
    };
    
    //  pointwise mapping 
    template<>
    struct BitsToTypesMap<32>{  typedef int TYPE;   };
    template<>
    struct BitsToTypesMap<8>{   typedef char TYPE;  };
    template<>
    struct BitsToTypesMap<16>{  typedef short TYPE; };
    
    //  cute wrapping
    template using MyScalarType = typename BitsToTypesMap::TYPE;
    
    //  TEST
    template
    MyScalarType
    Add ( MyScalarType x, MyScalarType y )
    {
        return x+y;
    }
    
    int
    test()
    {
        MyScalarType<32> i=Add<32>(1,2);
        MyScalarType<8 > b=Add<8 >(1,2);
        MyScalarType<16> s=Add<16>(1,2);
        return i+b+s;
    }
    

提交回复
热议问题