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.
AImpl
later on, but it's not clear why you don't do that directly viausing A = AImpl1<...>
. – BarbetAHelper
? It doesn't seem necessary in the code you show, so could you extend the example to include a purpose for theAHelper
part (which seems to be the core of the problem)? – BarbetdoSomething
for obvious reasons, but what code is it that works exactly the same for both fundamental types and class types? And how isCategory
needed in such a function for a plain integer? – Hoggish