How can I specialize typedef and its implicit type in different ways?

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 do not need to specialize in int, I really need to execute another function for AnotherType. And I cannot change the definition of AnotherType or the base function.

Overloading does not help either because of SFINAE.

+6
c ++ typedef templates specialization
source share
5 answers

You can use BOOST_STRONG_TYPEDEF .

+2
source share

The answer is no. When you enter typedef, you create an alias for the type, not the actual type itself. The compiler will refer to the same thing. That's why:

 typedef int Foo; typedef int Bar; Bar bar = 1; Foo foo = bar; 

Will compile. They are both ints.

+3
source share

I am sure that you cannot use the int compiler and AnotherType in different ways. All typedef β€” types of aliases β€” in fact, it does not create a new type; by typedef definition, the compiler will process int and AnotherType equivalently in all cases.

If you need a type with int that is handled differently, you just need to create a one-part struct . Most operations with int content will be compiled with the same machine code as the empty int, but now your data type may have its own specialized templates, etc.

+1
source share

And I cannot change the definition of AnotherType or the base function.

Then you are screwed. I'm sorry. The only option you have, a strong typedef, is not an option if you cannot change the definition to use a strong typedef.

+1
source share

The compiler will treat both specializations the same way, as AnotherType is just a different name for int . You say you don’t need to specialize in int , so just completely remove this specialization and let it specialize in any type of AnotherType that will work.

0
source share

All Articles