Is there a way a partial specialization is always preferred over the primary template?
-
09-02-2021 - |
Question
I'm asking myself
Can you write a class template and a corresponding partial specialization such that for any set of template arguments for the parameters, the partial specialization is taken by the compiler?
For example
template<typename T>
struct A { };
template<typename T>
struct A</* what to write!?*/> { };
I seem to remember having read that this is possible somehow, but I forgot the exact algorithm to make this work.
Solution
My version of GCC is happy to accept:
template<typename T>
struct A;
template<typename... Pack>
struct A<Pack...> {};
OTHER TIPS
If you allow SFINAE trick then, it will be as easy as this:
enum E { TRUE };
template<typename T, E = TRUE>
struct A
{
static const bool value = false;
};
template<typename T>
struct A<T, TRUE>
{
static const bool value = true;
};
Demo.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow