Reputation: 117771
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?
Upvotes: 6
Views: 710
Reputation: 120031
How do you tell a difference between a declaration that is a definition and a declaration that is not?
Easy: you can only have one definition of each entity, but as many declarations as you want.
Because you can say
typedef int Foo;
typedef int Foo;
without any problem, this is not a definition. Or perhaps because this is not a definition, you can say that. Either way, your compiler can easily tell which is which.
Note that in C it is illegal to repeat a typedef, so a typedef declaration is a definition in C.
Upvotes: 1
Reputation: 36451
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.
Upvotes: 6
Reputation: 6332
A typedef is a type alias, not a new type unto itself. With a typedef, nothing new is defined, but rather an existing definition is given a second name -- just like nightcracker is your alias, not your real name, but they both refer to the same entity: you.
Upvotes: 2
Reputation:
A definition creates an object, so:
int x;
is a definition of an int called x. A typedef does not create an object, it creates a new name for an existing type, so it is not a definition.
Upvotes: 0