|
From: Kakhkhor A. <kab...@gm...> - 2010-12-03 16:45:10
|
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;
}
|