Re: Temporary objects as constructor arguments?
From: Andy Buckley (buckley_at_hep.phy.cam.ac.uk)
Date: 08/25/04
- Next message: tt: "please help me figure out"
- Previous message: Howard: "Re: Error when compiling with Cygwin"
- In reply to: Victor Bazarov: "Re: Temporary objects as constructor arguments?"
- Next in thread: Victor Bazarov: "Re: Temporary objects as constructor arguments?"
- Reply: Victor Bazarov: "Re: Temporary objects as constructor arguments?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 25 Aug 2004 19:12:36 +0100
On Wed, 25 Aug 2004 18:27:23 +0100, Victor Bazarov wrote:
> Andy Buckley wrote:
>> As far as we can tell, all seven methods should successfully construct
>> a ROD object using temporary objects as constructor parameters, but
>> using g++ 3.2.3 only the first 4 actually succeed. The remainder seem
>> to be treated as constructing/declaring(?) a function pointer and so
>> the attempt to call a method fails for these.
>
> The last two are not declarations at all. The fifth is a declaration.
>> ROD rod5 ( T( t() ) ) ; // doesn't work
>
> It's a declaration. See FAQ 10.2 and search the Google Groups for
> similar problems people have been asking about since Adam.
Have looked at FAQ 10.2 and while rod5 is definitely being treated as a
declaration of a function which returns a ROD, I'm confies as to why:
the parentheses contain a real (if temporary) instantiation of a T object
rather than a type declaration. FAQ 10.2 only seems to describe the case
analogous to saying
ROD rod5();
which I can understand as declaring a function. Equivalently,
ROD rod5( T t( U u() ) );
according to the C++ function declaration grammar matches the definition
of a set of nested function declarations (however crazy the idea might
be). But when t() should return a concrete instatiation of a T object, I
would have expected this to fail to match the function declaration.
A clearer repost of the code without the copy-construction and
with the erroneous line-wrapping hopefully sorted is below. If you can
spare a moment to explain just what's going on, in particular on the
highlighted line, we'd both be very happy! Thanks.
// Reposted code:
#include <iostream>
typedef unsigned int U;
class T {
public:
T( const U & ) { }
};
class ROD {
public:
ROD(const T &) { }
void doSomething() const {
std::cout << "Hello -- I am a ROD" << std::endl;
}
};
class CC {
public:
void doSomethingElse() {
ROD rod5( T( u() ) ); // *** line of interest
rod5.doSomething(); // doesn't compile since it
// thinks rod5 is a function ptr
// of type ROD ()(T (*)())
}
U u() const { return 0; }
};
int main () {
CC cc;
cc.doSomethingElse();
return 0;
}
- Next message: tt: "please help me figure out"
- Previous message: Howard: "Re: Error when compiling with Cygwin"
- In reply to: Victor Bazarov: "Re: Temporary objects as constructor arguments?"
- Next in thread: Victor Bazarov: "Re: Temporary objects as constructor arguments?"
- Reply: Victor Bazarov: "Re: Temporary objects as constructor arguments?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|