A
Alexander Stippler
The following code snippet (those of you who read my last postings
might be familiar with it ;-)):
//--------------------------------------------------------------------
template<class Impl>
class Vector {};
template<class Ref>
class VectorView: public Vector<VectorView<Ref> >
{
Ref r;
public:
VectorView() {}
operator const Ref &() const { return r; }
operator Ref &() { return r; }
};
template<class T>
class DenseVector: public Vector<DenseVector<T> >
{
public:
DenseVector() {}
DenseVector(const DenseVector<T> &rhs) {}
VectorView<DenseVector<T> >
operator()(int from, int to)
{ return VectorView<DenseVector<T> >(); }
};
int fun(const DenseVector<double> &x) { return 1; }
int main()
{
DenseVector<double> x, y;
DenseVector<double> &w = x(1,3); // (1)
DenseVector<double> z = x(1,3); // (2)
fun(x(1,3)); // (3)
return 0;
}
//--------------------------------------------------------------------
What we are not able to comprehend is what happens in the three marked
lines:
In (2) x(1,3) creates an object of type VectorView<...> and its member
r is actually copied into z.
In (1) and (3) no such copying takes place.
Perhaps someone could tell us in detail what's going on internally in
those lines. Does it have to to with the copy constructor optimization
allowed by the standard in 12.8(15).
And if yes why does e.g. gcc 4.0 treat the three cases differently?
The standard does not enforce this optimization. But can it be
guaranteed that in (1) and (3) there will be no copying and that there
will always be copying in (2)?
Thanks
Alex
might be familiar with it ;-)):
//--------------------------------------------------------------------
template<class Impl>
class Vector {};
template<class Ref>
class VectorView: public Vector<VectorView<Ref> >
{
Ref r;
public:
VectorView() {}
operator const Ref &() const { return r; }
operator Ref &() { return r; }
};
template<class T>
class DenseVector: public Vector<DenseVector<T> >
{
public:
DenseVector() {}
DenseVector(const DenseVector<T> &rhs) {}
VectorView<DenseVector<T> >
operator()(int from, int to)
{ return VectorView<DenseVector<T> >(); }
};
int fun(const DenseVector<double> &x) { return 1; }
int main()
{
DenseVector<double> x, y;
DenseVector<double> &w = x(1,3); // (1)
DenseVector<double> z = x(1,3); // (2)
fun(x(1,3)); // (3)
return 0;
}
//--------------------------------------------------------------------
What we are not able to comprehend is what happens in the three marked
lines:
In (2) x(1,3) creates an object of type VectorView<...> and its member
r is actually copied into z.
In (1) and (3) no such copying takes place.
Perhaps someone could tell us in detail what's going on internally in
those lines. Does it have to to with the copy constructor optimization
allowed by the standard in 12.8(15).
And if yes why does e.g. gcc 4.0 treat the three cases differently?
The standard does not enforce this optimization. But can it be
guaranteed that in (1) and (3) there will be no copying and that there
will always be copying in (2)?
Thanks
Alex