Reputation: 369
I have something like this:
typedef int AnotherType;
template <typename T> Func( T Value );
// And I want to specialize these two cases separately:
template <> bool Func<int>( int Value ) {...}
template <> bool Func<AnotherType>( AnotherType Value ) {...}
I don't really need to specialize for int, what I really need is to execute a different function for AnotherType. And I cannot change the definition of AnotherType or the base function.
Overloading doesn't help either because of SFINAE.
Upvotes: 6
Views: 719
Reputation: 40859
And I cannot change the definition of AnotherType or the base function.
Then you're screwed. Sorry. The only option you really have, a strong typedef, is not an option if you can't change the definition to use a strong typedef.
Upvotes: 1
Reputation: 96241
The compiler will treat both of the specializations as exactly the same, since AnotherType
is just another name for int
. You say you don't need to specialize for int
, so just remove that specialization completely and let it specialize on whatever type AnotherType
happens to work out to.
Upvotes: 0
Reputation: 25271
I'm pretty sure you can't have the compiler treat int and AnotherType differently. All typedef does is alias types -- it doesn't actually create a new type; by definition of the typedef construct, the compiler will treat int and AnotherType equivalenty in all cases.
If you need to have a type with just an int that IS treated differently, you should probably just make a single-member struct
. Most operations on the contained int will compile to the same machine code as a bare int, but now your data type can have its own template specializations and such.
Upvotes: 1
Reputation: 35980
The answer is no. When you typedef you create an alias for a type, not an actual type in and of itself. The compiler will treat both the same. That's why:
typedef int Foo;
typedef int Bar;
Bar bar = 1;
Foo foo = bar;
Will compile. They're both ints.
Upvotes: 3