Disposable doesn't work?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Disposable doesn't work?

Kakhkhor Abdijalilov
Disposable template is supposed to eliminate unnecessary copy
construction when returning temporal objects from functions. On my
platform it actually prevents RVO (elide optimization) and triggers
copy construction, whereas without Disposable RVO eliminates
unnecessary copy construction.

Explicit assignment triggers copying with or without Disposable.

The code is below.

Regards,
Kakhkhor Abdijalilov.

//-------------------------------------------
/*
    Compile with optimization ON.
*/
#include <iostream>
#include <ql/utilities/disposable.hpp>

using namespace std;
using namespace QuantLib;

struct A {
    A() {}
    A(const A&) { cout << "Triggered copy ctor A(const A&)\n"; }

    A& operator=(const A&) {
        cout << "Triggered assignment A::=\n";
        return *this;
    }

    void swap(A&) {}
};

Disposable<A> f1() {
    A a;
    return a;
}

A f2() {
    A a;
    return a;
}

int main() {

    cout << "\nTesting RVO with Disposable...\n";
    A x1 = f1();
    cout << "\nTesting RVO without Disposable...\n";
    A x2 = f2();

    cout << "\nTesting explicit assignment with Disposable...\n";
    A y1;
    y1 = f1();
    cout << "\nTesting explicit assignment without Disposable...\n";
    A y2;
    y2 = f2();

    // Possible solution/guideline to assignment problem?
    cout << "\nUsing swap explicitly without Disposable...\n";
    A z;
    z.swap(f2());

    return 0;
}

------------------------------------------------------------------------------
Increase Visibility of Your 3D Game App & Earn a Chance To Win $500!
Tap into the largest installed PC base & get more eyes on your game by
optimizing for Intel(R) Graphics Technology. Get started today with the
Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs.
http://p.sf.net/sfu/intelisp-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: Disposable doesn't work?

Luigi Ballabio
On Fri, 2010-12-03 at 11:45 -0500, Kakhkhor Abdijalilov wrote:
> Disposable template is supposed to eliminate unnecessary copy
> construction when returning temporal objects from functions. On my
> platform it actually prevents RVO (elide optimization) and triggers
> copy construction, whereas without Disposable RVO eliminates
> unnecessary copy construction.
>
> Explicit assignment triggers copying with or without Disposable.

Of course it triggers assignment.  Your class must support Disposable
for it to work fully (see, e.g., the Array class.)  In your case, it
should be something like

struct A {
    A() {}
    A(const A&) { cout << "Triggered copy ctor A(const A&)\n"; }
    A(const Disposable<A>& a) { swap(const_cast<Disposable<A>&>(a)); }

    A& operator=(const A&) {
        cout << "Triggered assignment A::=\n";
        return *this;
    }
    A& operator=(const Disposable<A>& a) {
        swap(const_cast<Disposable<A>&>(a));
        return *this;
    }

    void swap(A&) {}
};

Hopefully, in a few years all compilers will support C++0X and we'll be
able to use rvalue references instead... (your class will still have to
support them. though.)

Luigi


--

For every problem there is one solution which is simple, neat, and
wrong.
-- H. L. Mencken



------------------------------------------------------------------------------
Increase Visibility of Your 3D Game App & Earn a Chance To Win $500!
Tap into the largest installed PC base & get more eyes on your game by
optimizing for Intel(R) Graphics Technology. Get started today with the
Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs.
http://p.sf.net/sfu/intelisp-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: Disposable doesn't work?

Kakhkhor Abdijalilov
So, this how it works. Thank you for clarification.

------------------------------------------------------------------------------
What happens now with your Lotus Notes apps - do you make another costly
upgrade, or settle for being marooned without product support? Time to move
off Lotus Notes and onto the cloud with Force.com, apps are easier to build,
use, and manage than apps on traditional platforms. Sign up for the Lotus
Notes Migration Kit to learn more. http://p.sf.net/sfu/salesforce-d2d
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev