Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specialize a typedef and its implicit type differently?

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.

like image 440
Frigg Avatar asked Jan 28 '11 20:01

Frigg


People also ask

What is the difference between typedef and #define?

typedef is limited to giving symbolic names to types only, whereas #define can be used to define an alias for values as well, e.g., you can define 1 as ONE, 3.14 as PI, etc. typedef interpretation is performed by the compiler where #define statements are performed by preprocessor.

What is the scope of a typedef?

A typedef is scoped exactly as the object declaration would have been, so it can be file scoped or local to a block or (in C++) to a namespace or class.

Does typedef create a new type?

Syntax. Note that a typedef declaration does not create types. It creates synonyms for existing types, or names for types that could be specified in other ways.

What is the use of typedef in C++?

The typedef keyword allows the programmer to create new names for types such as int or, more commonly in C++, templated types--it literally stands for "type definition". Typedefs can be used both to provide more clarity to your code and to make it easier to make changes to the underlying data types that you use.


1 Answers

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.

like image 151
wheaties Avatar answered Sep 25 '22 13:09

wheaties