templates - Assign to object being created in c++ -



templates - Assign to object being created in c++ -

i'm writing functions operate on vector data.

i defined objects non-copyable (private re-create constructor , assignment operator).

then defined templated operator =

template <typename g> inline const tmatrix &operator=(const g &gen) { ir_mat::copy<g, dimension>::start(m_data, gen); homecoming *this; }

and operators, '+', '*', described in article.

now can assign result of look object:

vector3f v1, v2, v3; v1 = v2 + v3;

why can't declare variable , assign in single statement?

vector3f v1, v2; vector3f v3 = v1 + v2;

is because assignment tries create temporary object before instantiating variable, , re-create new object? can utilize operator '=' instantiating new objects, without temporary storage? have define special constructor that?

update

i defined templated re-create constructor (in simplest form, probably):

template <typename g> tmatrix(const g &data) { operator=(data); }

now can instantiate v3 as:

vector3f v3(v1 + v2);

but still no luck other assignment:

vector3f v3 = v1 + v2;

this initialization:

vector v3(v1 + v2);

will phone call nice templated constructor.

this initialization:

vector v3 = v1 + v2;

ends getting translated this:

vector v3(vector(v1 + v2));

essentially compiler tries utilize re-create constructor , in doing attempts figure out how convert initializer appropriate type.

choosing create type that's assignable random types, can't re-create assigned or re-create constructed weird choice. see assumption you're doing avoid temporaries correct. except you're not doing because temporaries expensive exactly, because want building look evaluated later. presumably because want vectorize complex look evaluation.

yes, won't able utilize assignment form of initialization that. @ least, can't think of way create work.

my suggestion somehow transform vector beingness handle look you're building. handle handle real vector that's input. or handle vectorexpression that's result of operating on vectors.

if don't want shared ownership of objects have handles to, still won't able implement re-create assignment or re-create construction. in c++11 still implement move construction , move assignment of handle.

c++ templates operator-overloading

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -