Home:ALL Converter>Is it possible to make a template specialization to be equal to another type

Is it possible to make a template specialization to be equal to another type

Ask Time:2018-11-29T21:22:33         Author:Jarek C

Json Formatter

If I have e.g.:

template <class T>
class MyOptional { /*...*/ };

I know that I can define a specialization e.g. for T = bool which will have a different (separate) implementation:

template <> class MyOptional<bool> { /*...*/ };

But is it possible to say that this T=bool specialization equals to another type (e.g. MyOptBool)? Something like this:

class MyOptBool { /*...*/ };
using Optional<bool> = MyOptBool;  /* impossible but looking for similar functionality */

The only think I have found is using inheritance:

class MyOptBool { /*...*/ };
template <> class MyOptional<bool> : public MyOptBool {
  using MyOptBool::MyOptBool;
};

1st question: Is there any more elegant solution? (just for curiosity + I have some conversion issues, etc.).

2nd question: Is the "workaround" using inheritance for declaring two classes equal commonly used (in some important libraries, etc.)?

Author:Jarek C,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53540037/is-it-possible-to-make-a-template-specialization-to-be-equal-to-another-type
yy