Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguishing C++11 original type-name from typedef-name?

Tags:

c++

c++11

Is there anyway to distinguish the original name of a type from a typedef-name for that type?

For example:

class C1 {};

typedef C1 C2;

C1 and C2 both now name the same type. In code after the above, is there ever a case where an occurence of the name C1 refering to the type in question cannot be replaced with C2 (or visa versa)?

like image 872
Andrew Tomazos Avatar asked Jun 21 '13 14:06

Andrew Tomazos


People also ask

What is the difference between using and typedef?

A programmer in modern C++ has two options for declaring new type aliases. The typedef keyword is used to declare new type aliases in the typical way. The using keyword is the new means of declaring new type aliases introduced in C++11.

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.

How to use typedef in C++?

typedef in C++ typedef keyword is used to assign a new name to any existing data-type. For example, if we want to declare some variables of type unsigned int, we have to write unsigned int in a program and it can be quite hectic for some of us.

Are Typedefs scoped?

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.


1 Answers

A typedef creates an alias, and they are indistinguishable as a type. There are on the other hand some specific syntax constructs that require the real type and not the typedef-ed (declaration/definition of constructor/destructors...), but that is a different question. As a type they are indistinguishable.

like image 54
David Rodríguez - dribeas Avatar answered Nov 10 '22 00:11

David Rodríguez - dribeas