Tuesday, 15 April 2014

c++ - Return value optimization while returning by reference -



c++ - Return value optimization while returning by reference -

i have read lot of articles homecoming value optimization. yet i'm not sure understand if takes place in next case (the addresses identical):

#include "stdafx.h" class type { public: type(int a) { m_a = a; } private: type(const type & other) {} int m_a; }; class base of operations { public: base() : instance(42) {} virtual type & gettype() { printf("base: address of instance: %i\n", &instance); homecoming instance; } private: type instance; }; class derived : public base of operations { public: virtual type & gettype() { printf("derived\n"); homecoming base::gettype(); } }; int main() { printf("base class\n"); base of operations b; printf("main: address of instance: %i\n", &(b.gettype())); printf("\nderived class\n"); derived d; printf("main: address of instance: %i\n", &(d.gettype())); }

does returning reference ensure no re-create constructor called? or rvo taking place here?

does returning reference ensure no re-create constructor called?

yes. when pass or homecoming reference (as opposed passing/returning value), no re-create beingness constructed.

is rvo taking place here?

since homecoming reference, rvo has nil here. rvo optimization technique consists in elimination of redundant re-create beingness created while returning value.

rvo might take place if have function this:

type gettype() { type instance; //... homecoming instance; } //... type t = gettype();

then rvo compiler seek eliminate redundant copy constructor , destructor calls means local instance gettype function assigned variable t without re-create beingness created.

c++ reference return-value return-value-optimization return-by-reference

No comments:

Post a Comment