Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a typedef a definition?

Tags:

c++

I'm really confused. I am reading TC++PL by Bjarne Stroustrup (special edition, 19th print - Sep 2010). Let me quote a 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 can be seen from these examples, a declaration can do more than simply associate a type with a name. Most of these declarations are also definitions; that is, they also define an entity for the name to which they refer. For ch, that entity is the appropriate amount of memory to be used as a variable – that memory will be allocated. For day it is the specified function. For the constant pi, it is the value 3.1415926535897932385. For Date, that entity is a new type. For Point it is the type complex so that Point becomes a synonym for complex. Of the declarations above, only these are not also definitions:

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

Isn't the sentence in bold conflicting with the list given below it? Is a typedef just a declaration or also a definition? Is this an error in the book?

like image 385
orlp Avatar asked Apr 18 '11 15:04

orlp


People also ask

Is typedef the same as define?

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.

Is typedef user-defined?

typedef is a predefined keyword. You can replace the name of the existing data type with the name which you have provided. This keyword helps in creating a user-defined name for an existing data type. You can also use the typedef keyword with structures, pointers, arrays etc.

Why typedef better than definition?

The typedef has the advantage that it obeys the scope rules. That means you can use the same name for the different types in different scopes. It can have file scope or block scope in which declare. In other words, #define does not follow the scope rule.

Is typedef a variable?

A typedef declaration is interpreted in the same way as a variable or function declaration, but the identifier, instead of assuming the type specified by the declaration, becomes a synonym for the type.


1 Answers

Although I'm totally confused by this. The standard is clear. Typedef is just a declaration. Not definition.

3.1-2

A declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification24) (7.5) and neither an initializer nor a function-body, it declares a static data member in a class declaration (9.4), it is a class name declaration (9.1), or it is a typedef declaration (7.1.3), a using-declaration (7.3.3), or a using-directive (7.3.4).

Edit: Oh, I just realized why. You can typedef a declaration, therefore a typedef has to be a declaration itself.

like image 113
Šimon Tóth Avatar answered Sep 21 '22 22:09

Šimon Tóth