Is typedef a definition?

I'm really confused. I am reading TC ++ PL by Bjarne Stroustrup (special edition, 19th edition - September 2010). Let me quote part of the book, highlighting my confusion:

char ch; string s; int count = 1; const double pi = 3.1415926535897932385; extern int error_number; const char* name = "Njal"; const char* season[] = { "spring", "summer", "fall", "winter" }; struct Date { int d, m, y; }; int day(Date* p) { return p->d; } double sqrt(double); template<class T> T abs(T a) { return a<0 ? -a : a; } typedef complex<short> Point; struct User; enum Beer { Carlsberg, Tuborg, Thor }; namespace NS { int a; } 

As you can see from these examples, a declaration can do more than just associate a type with a name. Most of these statements are also definitions; that is, they also define the entity for the name to which they refer. For ch, this object is the appropriate amount of memory to be used as a variable - this memory will be allocated. For the day, this is a given function. For the constant pi, this value is 3.1415926535897932385. For date, this object is a new type. For a point, it is a complex of types, so that a point becomes a synonym for complex . Of the above ads, only these Definitions:

 double sqrt(double); extern int error_number; struct User; typedef complex<short> Point <-- WTF; 

Isn't this a bold sentence contrary to the list below? Is typedef just a declaration or also a definition? Is this a mistake in the book?

+6
source share
4 answers

Although I am completely confused by this. The standard is clear. Typedef is just a declaration. Not a definition.

3.1-2

A declaration is a definition if it declares a function without defining the function body (8.4), it contains the extern specifier (7.1.1) or the binding specifications 24) (7.5), and neither the initializer nor the function-body declares a static data member in the class declaration (9.4), this is a class name declaration (9.1), or it is a typedef declaration (7.1.3) , a use-declaration (7.3.3) or using-directive (7.3.4).

Edit: Oh, I just realized why. You can print the declaration, so typedef must be the declaration itself.

+6
source

typedef is a type alias, not a new type. Nothing new is defined with typedef, but a second name is given in place of the existing definition - just like a night scraper - it's your nickname, not your real name, but they both refer to the same entity: you.

+2
source

How do you distinguish between a declaration that is a definition and a declaration that is not?

Simple: you can have only one definition of each object, but as many ads as you want.

Because you can say

  typedef int Foo; typedef int Foo; 

without any problems, this is not a definition. Or perhaps because this is not a definition, you can say it. In any case, your compiler can easily determine what exactly.

Note that repeating typedef is not allowed in C, so typedef is a definition in C.

+1
source

The definition creates an object, therefore:

 int x; 

is an int definition called x. Typedef does not create an object; it creates a new name for an existing type, so this is not a definition.

0
source

Source: https://habr.com/ru/post/886157/


All Articles