lundi 29 février 2016

MSCV, constructor, destructor and NRVO behaviour

I was wondering if the NRVO was active on the project I was working on (which is Qt, using MSVC 2013 64 bit).

So I wrote this piece of code:

class foo
{
public:
    foo(){qDebug() << "foo::foo";}
    foo(const foo& c){(void)c;qDebug() << "foo::foo( const foo& )\n";}
    ~foo(){qDebug() << "foo::~foo";}
};
foo             bar()
{
    foo local_foo;
    return (local_foo);
}
void                    func()
{
    foo f = bar();
}

and it gave me the following output:

foo::foo

foo::~foo

Where the link I put above expects :

foo::foo()

foo::foo( const foo& )

foo::~foo()

foo::~foo()

But when I replace the bar call by

foo f = foo(bar())

then I get the same output that the links has.

So here's my question: why does "foo f = bar()" not call copy constructor? does it call the operator= instead, and before it is call, f is raw storage? (So why the link, which is from 2004, doesn't behave the same way)? So I must conclude NRVO isn't turned on, right?

Aucun commentaire:

Enregistrer un commentaire