Declaration or Definition

N

newmans

Perhaps one of the experts can straighten me out on this point...

In Bjarne Stroustrup's book 'The C++ Programming Language' 3rd Edition,
section 4.9, indicates that

typedef complex<short> Point;

is not only a declaration, but also a definition.

The ISO/IEC 14882:2003(E) standard states in section 3.1 clause 2 that

"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)".

So my question is , based on the (7.3.3) line of the clause, why is the
typedef
above a definition?


Newman
 
P

Phlip

newmans said:
Perhaps one of the experts can straighten me out on this point...

In Bjarne Stroustrup's book 'The C++ Programming Language' 3rd Edition,
section 4.9, indicates that

typedef complex<short> Point;

is not only a declaration, but also a definition.

The ISO/IEC 14882:2003(E) standard states in section 3.1 clause 2 that

"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)".

So my question is , based on the (7.3.3) line of the clause, why is the
typedef
above a definition?

Because it instantiates a template into a class at that point. The new class
bonds with identifiers seen at its location. Using Point later doesn't
re-bond these identifiers, as #define Point complex<short> would.

This is why one should always instantiate templates with typedefs, to
control their definition point.
 
R

Rob Williscroft

Phlip wrote in in
comp.lang.c++:
newmans said:
Perhaps one of the experts can straighten me out on this point...

In Bjarne Stroustrup's book 'The C++ Programming Language' 3rd
Edition, section 4.9, indicates that

typedef complex<short> Point;

is not only a declaration, but also a definition.

The ISO/IEC 14882:2003(E) standard states in section 3.1 clause 2
that

"A declaration is a definition unless [...]
or it is a typedef declaration (7.1.3), [...]
So my question is , based on the (7.3.3) line of the clause, why is
the typedef
above a definition?

The Standard says it isn't, nothing else matters.
Because it instantiates a template into a class at that point.

There is no class-template being instantiated here.
The new
class bonds with identifiers seen at its location. Using Point later
doesn't re-bond these identifiers, as #define Point complex<short>
would.

This is why one should always instantiate templates with typedefs, to
control their definition point.

This was true with some pre-standard version's of C++.

With Standard C++, the problem is /mostly/ mitigated with
2-phase name lookup, if that isn't good enough you can use:

template complex< short >;

But it only works if *all* of complex<>'s member's are
instantiatable with the 'short' argument.

Rob.
 
C

Chris Gordon-Smith

newmans said:
Perhaps one of the experts can straighten me out on this point...

In Bjarne Stroustrup's book 'The C++ Programming Language' 3rd Edition,
section 4.9, indicates that

typedef complex<short> Point;

is not only a declaration, but also a definition.

The ISO/IEC 14882:2003(E) standard states in section 3.1 clause 2 that

"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)".

So my question is , based on the (7.3.3) line of the clause, why is the
typedef
above a definition?

Interesting question. Here is my understanding of the situation.

Section 4.9 of Stroustrup's book says "definitions [...] also define an
entity for the name to which they refer. ... For Point it is the type
complex<short> so that point becoms a synonym for complex<short>".

Therefore, the definition part of this typedef is that it is defining a new
type (the 'entity'), complex<short>.

The type complex<short> would not exist without the typedef.

I assume that the line of the standard you intended to mention was 7.1.3.
This says that a typedef declaration is not a definition. I would think
that an example of this would be

typedef int int32;

This is not defining an 'entity'. In this case, the entity is the built in
type int, which already exists. Therefore this typedef is just declaring
the name int32 as a synonym for the already existing type 'int'.
 
A

Alf P. Steinbach

* newmans:
Perhaps one of the experts can straighten me out on this point...

In Bjarne Stroustrup's book 'The C++ Programming Language' 3rd Edition,
section 4.9, indicates that

typedef complex<short> Point;

is not only a declaration, but also a definition.

The ISO/IEC 14882:2003(E) standard states in section 3.1 clause 2 that

"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)".

So my question is , based on the (7.3.3) line of the clause, why is the
typedef above a definition?

It isn't.

How exactly does Bjarne, in your opinion, "indicate" that it is?
 
R

Russell Hanneken

Alf said:
* newmans:
How exactly does Bjarne, in your opinion, "indicate" that it is?

After giving a list of declarations, including

typedef complex<short> Point;

Stroustrup writes:

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. . . . For
day, it is the specified function. . . . For Point, it is the type
complex<short> so that Point becomes a synonym for complex<short>. Of
the declarations above, only

double sqrt(double);
extern int error_number;
struct User;

are not also definitions; that is, the entity they refer to must be
defined elsewhere.

Seems pretty clear to me. I'm quoting from the 3rd printing of the "special
edition." I checked Stroustrup's errata pages to see if he corrected
himself in a later printing, but I don't see any mention of this issue.
 
R

Russell Hanneken

Chris said:
The type complex<short> would not exist without the typedef.

I assume that the line of the standard you intended to mention was 7.1.3.
This says that a typedef declaration is not a definition. I would think
that an example of this would be

typedef int int32;

This is not defining an 'entity'. In this case, the entity is the built in
type int, which already exists. Therefore this typedef is just declaring
the name int32 as a synonym for the already existing type 'int'.

Interesting distinction, but unfortunately the standard says "A declaration
is a definition unless . . . it is a typedef declaration." For your
argument to work, you would have to say that

typedef complex<short> Point;

is a definition, but not a typedef declaration. I don't think the standard
means to say that.
 
A

Alf P. Steinbach

* Russell Hanneken:
After giving a list of declarations, including

typedef complex<short> Point;

Stroustrup writes:

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. . . . For
day, it is the specified function. . . . For Point, it is the type
complex<short> so that Point becomes a synonym for complex<short>. Of
the declarations above, only

double sqrt(double);
extern int error_number;
struct User;

are not also definitions; that is, the entity they refer to must be
defined elsewhere.

Seems pretty clear to me. I'm quoting from the 3rd printing of the "special
edition." I checked Stroustrup's errata pages to see if he corrected
himself in a later printing, but I don't see any mention of this issue.

Urm, well.

The only practical difference seems to be that while you cannot have
multiple definitions of the same name (except internal linkage in
different compilation units), you can have multiple typedef's of the
same name provided they denote the same type.

I'm forwarding this to Bjarne, assuming his spam-filter doesn't
automatically trash mails from me... ;-)
 
C

Chris Gordon-Smith

Russell said:
Interesting distinction, but unfortunately the standard says "A
declaration
is a definition unless . . . it is a typedef declaration." For your
argument to work, you would have to say that

typedef complex<short> Point;

is a definition, but not a typedef declaration. I don't think the
standard means to say that.

Fair enough. But that does seem to leave us in a rather contradictory
situation:

1) typedef int int32; // Declaration that is not a definition
2) typedef complex<short> Point; // Declaration that is also a
definition (as stated by Stroustrup section 4.9)
3) The standard says "A declaration is a definition unless . . . it is a
typedef declaration."

(2) and (3) can only be consistent if (2) is a declaration (and a
definition), but not a 'typedef declaration'.

Any ideas on a way out of this contradictory situation? What is a 'typedef
declaration'?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top