Posted by
Kakhkhor Abdijalilov on
URL: http://quantlib.414.s1.nabble.com/Disposable-doesn-t-work-tp12414.html
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