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