How can I provide template specializations for typedefs of the same type?

后端 未结 4 549
长情又很酷
长情又很酷 2021-01-20 02:49

A 3rd party SDK defines several typedefs, e.g.:

typedef unsigned char SDK_BYTE
typedef double SDK_DOUBLE
typedef unsigned char SDK_BOOLEAN

4条回答
  •  不知归路
    2021-01-20 03:14

    If C++11 is an option for you, here is some code illustrating a possible solution using std::enable_if and std::is_same:

    #include 
    #include 
    
    struct SdkVariant
    {
    };
    
    typedef int   type1;
    typedef float type2;
    
    template 
    class variant_cast
    {
    public:
      /* Default implementation of the converter. This is undefined, but
         you can define it to throw an exception instead. */
      T operator()(const SdkVariant &v);
    };
    
    /* Conversion for type1. */
    template 
    class variant_cast::value>::type>
    {
    public:
      type1 operator()(const SdkVariant &v)
      {
        return type1 { 0 };
      }
    };
    
    /* Conversion for type2, IF type2 != type1. Otherwise this
       specialization will never be used. */
    template 
    class variant_cast::value
          && !std::is_same::value>::type>
    {
     public:
      type2 operator()(const SdkVariant &v)
      {
        return type2 { 1 };
      }
    };
    
    int main()
    {
      variant_cast vc1;
      variant_cast vc2;
      std::cout << vc1({}) << std::endl;
      std::cout << vc2({}) << std::endl;
      return 0;
    }
    

    A few notes:

    1. Instead of the various types you get defined by that library, I have only defined type1 and type2
    2. I have defined an empty SdkVariant struct as a dummy
    3. Because that dummy is empty, my conversion does not really convert anything. It just outputs a constant (value 0) when converting to type1, and a constant (value 1) when converting to type2 (if type2 is actually different from type1).
    4. To test whether it does what you need, you may replace the definition of type2 with

      typedef int type2;
      

      so it is identical with the definition for type1. It will still compile, and there will be no error related to any double definition.

    5. I have tested this using GCC 4.7.0 and the --std=c++11 option.

    Remark about the use of std::enable_if and partial vs. explicit template specializations

    The converter for type1 is declared as

    template 
    variant_cast::value>::type>
    

    which means it is defined for any type T that is the same as type1. Instead, we could have used an explicit specialization

    template <>
    variant_cast
    

    which is much simpler, and works, too.

    The only reason I didn't do that is that in the case of type2 it won't work, because for type2 we must check whether it is the same as type1, i.e. we must use std::enable_if:

    template <>
    class variant_cast::value>::type>
    

    Unfortunately, you can't use std::enable_if in an explicit specialization, because an explicit specialization is not a template – it's a real data type, and the compiler must process it. If type1 and type2 are identical, this:

    typename std::enable_if::value>::type
    

    does not exist, because of the way std::enable_if works. So the compilation fails because it can't instantiate this data type.

    By defining the converter for any type T that is the same as type2 we avoid the explicit instantiation for type2, hence we don't force the compiler to process it. It will only process the template specialization for type2 if the std::enable_if<...>::type actually exists. Otherwise it will simply ignore it, which is exactly what we want.

    Again, in the case of type1 (and for any further type3, type4 etc.) an explicit instantiation will work.

    I think it's worthwhile pointing out that defining a template specialization for any type T that is the same as some type type is a trick that is generally applicable whenever you can't use an explicit specialization for formal reasons, so you use a partial specialization, but you really want to bind it to this one type only. For instance, a member template cannot be explicitly instantiated unless its enclosing template is explicitly instantiated, too. Using a combination of std::enable_if and std::is_same probably helps there, too.

提交回复
热议问题