Topic: Tricky expression
Author: Kin H Chan <firstian@MIT.EDU>
Date: 1997/03/13 Raw View
I have the following setup
class A {
public:
A() {} // default ctor
A(const A &a) {...} // copy ctor
~A() {...}
...
};
class B {
public:
B(const A &a) : data(a) {}
private:
A data;
};
Now I have an expression like this
B some_B( A() );
Is this suppose to be (1) a declaration of an instance of B and pass a
temporary A obtained from the default constructor as the constructor
argument of some_B. (2) A declaration of a function some_B, taking a
function ptr of type A (*)(void) as argument, and return a B object? I
have two compilers, and these are the two different results they gave
me. In any case, what is the rule to parse such an expression? Where is
that outlined in the draft standard? And how come A() can be used to
declare a function ptr, if the second interpretation is correct? Again,
I'll be grateful if someone can tell me where to look in the draft
standard.
Thanks
Kin Chan
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/03/14 Raw View
Kin H Chan writes:
> B some_B( A() );
> Is this suppose to be (1) a declaration of an instance of B and pass a
> temporary A obtained from the default constructor as the constructor
> argument of some_B. (2) A declaration of a function some_B, taking a
> function ptr of type A (*)(void) as argument, and return a B object?
(2)
> In any case, what is the rule to parse such an expression? Where is
> that outlined in the draft standard? And how come A() can be used to
> declare a function ptr, if the second interpretation is correct? Again,
> I'll be grateful if someone can tell me where to look in the draft
> standard.
8.2 Ambiguity resolution [dcl.ambig.res]
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/03/14 Raw View
Kin H Chan <firstian@MIT.EDU> writes:
|> I have the following setup
|>
|> class A {
|> public:
|> A() {} // default ctor
|> A(const A &a) {...} // copy ctor
|> ~A() {...}
|> ...
|> };
|>
|> class B {
|> public:
|> B(const A &a) : data(a) {}
|>
|> private:
|> A data;
|> };
|>
|>
|> Now I have an expression like this
|>
|> B some_B( A() );
|>
|> Is this suppose to be (1) a declaration of an instance of B and pass a
|> temporary A obtained from the default constructor as the constructor
|> argument of some_B. (2) A declaration of a function some_B, taking a
|> function ptr of type A (*)(void) as argument, and return a B object?
I don't see where this interpretation is legal. How can A() be the
declaration of a function pointer?
|> I
|> have two compilers, and these are the two different results they gave
|> me. In any case, what is the rule to parse such an expression?
The rule is simple: if it can be parsed as a declaration, it is. And in
fact, you do have to watch out for explicit constructors which take a
single argument; they often can be parsed as a declaration, which
converts the entire statement into a declaration. In this case,
however, there is no way that "A()" can be parsed as a declaration, so
it isn't one. And if "A()" isn't a declaration, it must be an
initializer.
Note that with the implicit int rule, "A()" could be the
declaration of a function. But the implicit int rule has never been
applicable within a function prototype, and so could not apply above.
Even if it could, of course, the result would be a function which takes
a function -- not a pointer to a function -- as an argument, and such
things don't exist.
Alternatively, and this might be what is happening in the second
compiler: "A()" *is* a typename (not a declaration) for a function
taking no arguments and returning an A. Again: it is not a typename for
a pointer to a function, but for the function itself. And a typename
for a function is not legal in this context. (Is there a context where
it is legal? Off hand, typenames are legal in function prototypes, new
expressions and sizeof expressions. In none of these cases is a
function a legal argument.)
|> Where is
|> that outlined in the draft standard? And how come A() can be used to
|> declare a function ptr, if the second interpretation is correct? Again,
|> I'll be grateful if someone can tell me where to look in the draft
|> standard.
Well, I'd start with the syntax for declarations.
--
James Kanze home: kanze@gabi-soft.fr +33 (0)1 39 55 85 62
office: kanze@vx.cit.alcatel.fr +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
-- Conseils en informatique industrielle --
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]