By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,412 Members | 1,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,412 IT Pros & Developers. It's quick & easy.

Declaration or Definition

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
newmans wrote:
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.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2

P: n/a
Phlip wrote in news:dv****************@newssvr33.news.prodigy.com in
comp.lang.c++:
newmans wrote:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

P: n/a
newmans wrote:
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'.

--
Chris Gordon-Smith
London
Homepage: http://graffiti.virgin.net/c.gordon-smith/
Email Address: Please see my Home Page
Jul 22 '05 #4

P: n/a
* 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?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #5

P: n/a
Alf P. Steinbach wrote:
* newmans:

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.

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.

--
Russell Hanneken
eu*******@cbobk.pbz
Use ROT13 to decode my email address.
Jul 22 '05 #6

P: n/a
Chris Gordon-Smith wrote:
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.

--
Russell Hanneken
eu*******@cbobk.pbz
Use ROT13 to decode my email address.


Jul 22 '05 #7

P: n/a
* Russell Hanneken:
Alf P. Steinbach wrote:
* newmans:

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.

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.


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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #8

P: n/a
Russell Hanneken wrote:
Chris Gordon-Smith wrote:
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.


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'?

--
Chris Gordon-Smith
London
Homepage: http://graffiti.virgin.net/c.gordon-smith/
Email Address: Please see my Home Page
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.