Re: Temporary objects as constructor arguments?

From: Andy Buckley (buckley_at_hep.phy.cam.ac.uk)
Date: 08/25/04


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;
}



Relevant Pages

  • Re: void : bug or feature?
    ... X_To_2are entirely contained inside the scope of ClassA's definition. ... >> Note the line indicated by the asterisks above begins with a 'void'. ... > The indicated line is simply a function declaration. ...
    (microsoft.public.dotnet.languages.vc)
  • [PATCH] update checkpatch.pl to version 0.17
    ... This version brings improvements to external declaration detection, ... to quote tracking, fixes to unary tracking, some clarification of wording, ... detect external function declarations without an extern prefix ... function declaration arguments should be with the identifier ...
    (Linux-Kernel)
  • Confusion in ANSI Cs function concepts
    ... is no function declaration corresponding to the function call and the ... call does not say anything about the return type then the compiler ... in the above case the return type is mentioned as void *. ...
    (comp.lang.c)
  • Re: Pointer to an array of pointers
    ... Be very careful when using this function declaration since you don't know whether you're calling the ASCII or Unicode ... > Yes, i tried the Mike solution, but it is crashing VB6. ...
    (microsoft.public.vb.general.discussion)