Declaration or Definition

Discussion in 'C++' started by newmans, Jun 26, 2004.

  1. newmans

    newmans Guest

    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
    newmans, Jun 26, 2004
    #1
    1. Advertising

  2. newmans

    Phlip Guest

    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/bin/view/Main/TestFirstUserInterfaces
    Phlip, Jun 26, 2004
    #2
    1. Advertising

  3. Phlip wrote in news:dv5Dc.1291$ 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/
    Rob Williscroft, Jun 26, 2004
    #3
  4. 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
    Chris Gordon-Smith, Jun 26, 2004
    #4
  5. * 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?
    Alf P. Steinbach, Jun 26, 2004
    #5
  6. 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

    Use ROT13 to decode my email address.
    Russell Hanneken, Jun 26, 2004
    #6
  7. 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

    Use ROT13 to decode my email address.
    Russell Hanneken, Jun 26, 2004
    #7
  8. * 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?
    Alf P. Steinbach, Jun 26, 2004
    #8
  9. 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
    Chris Gordon-Smith, Jun 27, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Razvan
    Replies:
    4
    Views:
    620
    Razvan
    Feb 10, 2005
  2. Jianli Shen
    Replies:
    1
    Views:
    587
    Victor Bazarov
    Mar 13, 2005
  3. Ark
    Replies:
    1
    Views:
    416
    Chris Torek
    Aug 7, 2004
  4. Jon Slaughter
    Replies:
    4
    Views:
    449
    Jon Slaughter
    Oct 26, 2005
  5. Pierre Yves
    Replies:
    2
    Views:
    484
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page