Mixing aliases and template specializations

扶醉桌前 提交于 2019-12-06 11:48:15

问题


I'm trying to find the best method to have a kind of "object" that can be either specialized or "linked" to another type.

For instance you cannot specialize a class to make it become a simple int, and you cannot use the keyword using to specialize classes.

My solution is the following:

template<class Category, Category code>
struct AImpl
  {};

template<class Category, Category code>
struct AHelper
  {
  using type = AImpl<Category, code>;
  };

template<class Category, Category code>
using A = typename AHelper<Category, code>::type;

template<int code>
void doSomething(A<int, code> object)
  {
  }

template<>
struct AImpl<int, 5>
  { 
  double a; 
  };

template<>
struct AImpl<int, 6>
  { 
  int b; 
  double c;
  };

template<>
struct AHelper<int, 7>
  {
  using type = int;
  };

template<class Category, Category code>
struct Alternative {};

template<int code>
void doSomethingAlternative(Alternative<int, code> object)
  {
  }

This works but you need to specify the code parameter in doSomething, and I would like to avoid that.

For instance:

A<int,7> a7; // This is equivalent to int
a7 = 4;
A<int, 5> a5; // This is equivalent to AImpl<int,5>
a5.a = 33.22;
doSomething(a5); // This does not compile
doSomething<5>(a5); // This compiles but is bulky
Alternative<int,0> alt0;
doSomethingAlternative(alt0); // This compiles and is not bulky
                              // but you're forced to use class
                              // specializations only

Is there a way to achieve what I want? It's ok to change both doSomething or the A implementation.


回答1:


If you are trying to customize the behavior of doSomething based on the type it is called with, you cannot have the compiler deducing stuff from AHelpr::type (as previously answered). But you can tell it what it needs to know by providing a customization point in the form of traits. For instance:

template<typename T>
void doSomething(T& object)
{
    auto code = SmthTriats<T>::code;
}

This is highly extendable given the ability to specialize SmthTriats:

template<typename> struct SmthTriats;

template<typename Category, Category code_>
struct SmthTriats<AImpl<Category, code_>> {
    static constexpr auto code = code_;
};

template<>
struct SmthTriats<int> {
    static constexpr auto code = 7;
};

A traits class allows for customization outside your module too. Client code need only specialize SmthTriats with their own type, and so long as they respect the contract you laid out with your trait, your code will work for them.



来源:https://stackoverflow.com/questions/55969287/mixing-aliases-and-template-specializations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!