Boost logo

Boost :

From: Alkis Evlogimenos (alkis_at_[hidden])
Date: 2002-07-02 09:36:40


Daniel Frey wrote:

> "Victor A. Wagner, Jr." wrote:
>>
>> "missed"? Unnamed return value optimization has been around longer.
>> Scott Meyers (I left his books at the office, so I can't quote which)
>> talked at length about why naming the temporary was likely to generate
>> WORSE code on most (at the time he wrote it) compilers.
>
> It's "More Efficient C++", Item 4.7. Please also read the errata,
> available at Scott's website. I haven't seen a single case where a
> compiler optimized away any of the unnamed return values for all the
> relevant tests (see csc++). I checked this for GCC 2.95.2, GCC 2.95.3,
> GCC 3.0.4, GCC 3.1, TenDRA 4.1.2, KAI CC (1 year ago, don't remember the
> version), Sun's CC and IIRC the HP-compiler (version: see KAI CC).
> Before the GCC 3.1, it made no difference for me as all compilers I know
> of didn't optimized the return value. With GCC 3.1, things changed and
> it was the first time I was able to remove the temporary with standard
> C++. The GCC 2.95.x had a special extension, which allowed something
> similar, but that was not portable. Still I'd like to hear about a
> single compiler that actually performs the RVO for Scott's example...
> (and from the thread in csc++, it isn't clear if it is allowed to be
> optimized :)
>

Running this code:

#include <cstdio>

using namespace std;

struct Foo
{
        Foo() { printf("constructor\n"); }
        Foo(const Foo& rhs) { printf("copy constructor\n"); }

        static Foo createFooNamed() {
                Foo f;
                return f;
        }
        static Foo createFooUnnamed() {
                return Foo();
        }
};

int main(int argc, const char** argv)
{
        printf("Named:\n");
        Foo a = Foo::createFooNamed();
        printf("Unnamed:\n");
        Foo b = Foo::createFooUnnamed();
}

gcc 2.96 without any optimizations gives the following output:
Named:
constructor
copy constructor
Unnamed:
constructor

gcc 3.1 without any optimizations gives this output:
Named:
constructor
Unnamed:
constructor

It seems that gcc 2.96 optimizes away the unmaned temp but not the
named one, whereas gcc3.1 optimizes both... Or, am I missing something?

Alkis Evlogimenos

> Regards, Daniel
>
> --
> Daniel Frey
>
> aixigo AG - financial training, research and technology
> Schloß-Rahe-Straße 15, 52072 Aachen, Germany
> fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
> eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk