Method stack return usage has incorrect GCC error

Arthur Schwarz aschwarz1309@att.net
Fri Jul 6 17:22:00 GMT 2012


IDE Netbeans 7.2RC1   GCC 4.5.3 compiled under Cygwin

In the following example I think that gcc has incorrectly generated an "error: 
no matching function for call to ‘derived::fnc4(derived)’" error for " 
b1.fnc4(b1.fnc1());". My guess is that gcc does not handle a function return 
returning an object on the stack used as an argument to a function. If this is 
incorrect, on my part, would someone explain why I am wrong.

The code contains several examples of usage for function arguments. When a 
stacked object, derived b1(++y);"" and  ' derived  b3 = b1.fnc1();'",  is used 
as an argument to a function,  b1.fnc4(b3);"" and " b1.fnc4(b1);",  no error is 
generated. The diagnostic only occurs when the return argument fro a function is 
used as an argument to another function.

Not that this is of the slightest concern to GCC (nor should it be), MSVC++ 2010 
does not produce a diagnostis.

thanks
art


# include <cstdlib>
# include <iostream>
# include <iomanip>

using namespace std;

static int y = 0;

class base {
public:
    base* linkLeft;
    base* linkRight;
    int   x;
    base()        { cout << "base (copy constructor call)\n" ; }
    base(int x)   { this->x = x; cout << "Base "; output(); };
    ~base()       { cout << "base deconstructor for " << x << endl;}
    base(const base& copy) { cout << "base copy constructor for " << x << "\n"; 
}
    void output() {
        cout << "Constructor " << x << "  <"
             << setw(8) << setfill('0') << hex << linkLeft  << "  "
             << setw(8) << setfill('0') << hex << linkRight << ">"
             << endl;
             if (linkLeft != linkRight) linkLeft = linkRight = NULL;
    } // output
    void * operator new(size_t size) { base* cell = (base*)malloc(sizeof(base));
                                       cell->linkLeft = cell->linkRight = 
(base*)0xDeadBeef; 

                                       return (void*)cell; }
    void   operator delete(void* ptr) { free(ptr); }
};

class derived : public base {
public:
    derived(int x) :base(x)   { cout << "derived constructor " << x << "\n";}
    ~derived()                { cout << "derived deconstructor for " << x << 
"\n"; }
    derived(const derived& copy) { cout << "derived copy constructor for " << x 
<< "\n"; }
    derived  fnc1()           { cout << "fnc1\n"; return derived(++y); }
    derived& fnc2()           { cout << "fnc2\n"; return *(new derived(++y)); }
    void     fnc4(derived& d) { cout << "fnc4\n"; }
    void     fnc5(derived  d) { cout << "fnc5\n"; }
    
};

int main(int argc, char** argv) {
    {
        derived b1(++y);
        derived* b2 = new derived(++y);
        derived  b3 = b1.fnc1();
        
        b1.fnc4(b1.fnc1());    // error: no matching function for call to 
‘derived::fnc4(derived)’
        b1.fnc4(b3);
        b1.fnc4(b1);
        b1.fnc4(*b2);
        
        b1.fnc5(b1.fnc1());
        b1.fnc5(b1);
        b1.fnc5(*b2);
    }

    return 0;
}



More information about the Gcc-help mailing list