Topic: Is this a function declaration ?


Author: Michiel Salters <salters@lucent.com>
Date: 1999/12/22
Raw View
Dirk van Deun wrote:

> Stroustrup says that when disambiguating constructions that could be
> declarations, but could also be initialisations, you should choose
> the former interpretation.  A well-known `hard' example is:

>   someClass x (int(a), int(b), int(c)+2);

Not wanting to boot up my standard, I'm going to do something more
dangerous than dereferencing a null pointer: I'm going to guess
what the standard says:

Using the syntax, we can determine that:
- The last part, int(c)+2 can't be a statement, and
- it can't be an argument in a declaration, but
- it can be an expression.
So, x(...) must be an initialisation.

> which must be an initialisation, although you only notice it at the
> addition.  Now does this also mean that this is an initialisation:

>   someClass x (int(a), int(b), int(a));

Now, int(a) can be an argument in a declaration
according to the syntax. The fact that it is illegal
is only detected in a later phase, when the syntactically
correct program has a declararation with duplicate
parameters.

> The repeated int(a) should make it impossible for this line of code
> to be a correct function declaration.  So by the rule, this must be
> an object initialisation.  I however find it hard to believe that
> the parser should notice repeated arguments, so I would like to
> know if this is no exception, to be interpreted as `an incorrect
> function declaration.'

The rule is about syntax errors only. If a statement taken as an
initialisation would be a _syntax_ error, it must be a declaration.

> (I don't have a more formal standard document than the appendices to
> Stroustrup's book.)

I do. Finding these things is hard, even if you have a PDF copy.
Must be a question of spending a lot of time with them, but
who gets time to do so?

Michiel Salters
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: dvandeun@vub.ac.be (Dirk van Deun)
Date: 1999/12/20
Raw View
Stroustrup says that when disambiguating constructions that could be
declarations, but could also be initialisations, you should choose
the former interpretation.  A well-known `hard' example is:

  someClass x (int(a), int(b), int(c)+2);

which must be an initialisation, although you only notice it at the
addition.  Now does this also mean that this is an initialisation:

  someClass x (int(a), int(b), int(a));

The repeated int(a) should make it impossible for this line of code
to be a correct function declaration.  So by the rule, this must be
an object initialisation.  I however find it hard to believe that
the parser should notice repeated arguments, so I would like to
know if this is no exception, to be interpreted as `an incorrect
function declaration.'

(I don't have a more formal standard document than the appendices to
Stroustrup's book.)

Dirk van Deun                         Ceterum censeo Redmond delendum
--
Currently at: dirk@igwe.vub.ac.be  http://student.vub.ac.be/~dvandeun
Permanent mail forwarder: dvandeun@poboxes.com
--
Knight of the Order of the Command Line  ---  http://knights.rave.org
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]