473,382 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

copy constructor - optimized away or not?

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
Oct 30 '05 #1
5 1867
Alexander Stippler wrote:
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.


In (1) you explicitly ask the compiler not to copy anything. `w` is a reference.

In (2) the compiler is allowed to optimise away the copy. I guess g++ was confused by the
fact that the type you return is different from the type you copy to.

In (3) you pass the new object to "fun" by reference, so again you explicitly ask it not
to copy.

You would understand your own code better if you had a simple example without all these
templates which only obscure the real problem.

--

Valentin Samko - http://www.valentinsamko.com
Oct 30 '05 #2
In <43***********************@authen.white.readfreene ws.net> Valentin
Samko wrote:
Alexander Stippler wrote:
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.
In (1) you explicitly ask the compiler not to copy anything. `w` is a
reference.

In (2) the compiler is allowed to optimise away the copy. I guess g++
was confused by the fact that the type you return is different from
the type you copy to.

In (3) you pass the new object to "fun" by reference, so again you
explicitly ask it not to copy.


But in (2) I pass the object by reference too. No difference to (3)
with respect to this.
You would understand your own code better if you had a simple example
without all these templates which only obscure the real problem.

Template code has different behavior in several areas. So I do not
want to understand it for non-template code and then recognize that
template code behaves diffent. And the templates do not add complexity
here. And in one place in the code they are not superfluous, but
essential to realize the Barton-Nackman-Trick.
--

Valentin Samko - http://www.valentinsamko.com

Oct 30 '05 #3
Alexander Stippler wrote:
DenseVector<double> x, y;
DenseVector<double> &w = x(1,3); // (1)
DenseVector<double> z = x(1,3); // (2)
fun(x(1,3)); // (3)
In (2) the compiler is allowed to optimise away the copy. I guess g++
was confused by the fact that the type you return is different from
the type you copy to.

In (3) you pass the new object to "fun" by reference, so again you
explicitly ask it not to copy.


But in (2) I pass the object by reference too. No difference to (3)
with respect to this.

I do not see any references in the (2) code path. What do you mean by "I pass the object
by reference too"?

--

Valentin Samko - http://www.valentinsamko.com
Oct 30 '05 #4
In <43***********************@authen.white.readfreene ws.net> Valentin
Samko wrote:
Alexander Stippler wrote:
DenseVector<double> x, y;
DenseVector<double> &w = x(1,3); // (1)
DenseVector<double> z = x(1,3); // (2)
fun(x(1,3)); // (3) In (2) the compiler is allowed to optimise away the copy. I guess
g++ was confused by the fact that the type you return is different
from the type you copy to.

In (3) you pass the new object to "fun" by reference, so again you
explicitly ask it not to copy.


But in (2) I pass the object by reference too. No difference to (3)
with respect to this.

I do not see any references in the (2) code path. What do you mean by
"I pass the object by reference too"?


The signature of fun is void fun(const DenseVector<double> &x), so
the argument is a const &. And the copy constructor for (2) expects
a const & too. Where am I wrong?
Oct 30 '05 #5
Alexander Stippler wrote:
> DenseVector<double> x, y;
> DenseVector<double> &w = x(1,3); // (1)
> DenseVector<double> z = x(1,3); // (2)
> fun(x(1,3)); // (3)
In (2) the compiler is allowed to optimise away the copy. I guess
g++ was confused by the fact that the type you return is different
from the type you copy to.

In (3) you pass the new object to "fun" by reference, so again you
explicitly ask it not to copy.

But in (2) I pass the object by reference too. No difference to (3)
with respect to this.

I do not see any references in the (2) code path. What do you mean by
"I pass the object by reference too"?

The signature of fun is void fun(const DenseVector<double> &x), so
the argument is a const &. And the copy constructor for (2) expects
a const & too. Where am I wrong?


1. Yes, you have a reference in the copy constructor, I missed that.

2. operator() returns object by value, this leads to one temporary (which may be optimised
away, this depends on implementation of operator ()).

3. this has nothing to do with the function "fun" as it is not called in (2).

4. In addition to a possible temporary, you are creating a new object "z" which will
contain a copy of that temporary.

--

Valentin Samko - http://val.samko.info
Oct 30 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Shea Martin | last post by:
I have a String class (I know I am re-inventing the wheel, yes I have heard of boost, and of QString). My copy constructor does a deep (strcpy) of the char *_buffer member. I have a member...
5
by: LRS Kumar | last post by:
Stoustrup, in The C++ Programming Language, has the following example in 11.7: string g (string arg) // string passed by value using copy constructor { return arg; //string...
3
by: bipod.rafique | last post by:
Hello all, Even though this topic has been discussed many times, I still need your help in clearing my confusions. Consider the following code: class aclass{ public: aclass(){
7
by: David Lindauer | last post by:
If I do this: struct aa { int r,s,t; aa(); aa(const aa& t); }; int tt()
6
by: Dhirendra Singh | last post by:
Hi, In the below example, does copy constructor has any role in the initialization ? class complex { private: double re, im; public: complex( double r = 0, double i = 0) :re(r), im(i) {}...
9
by: janzon | last post by:
Consider the code below. The output is the following two lines: 0xbfc78090 0xbfc780a0 This proves that the variable m in main() is not the very same instance of MyClass as temp_m in hello()....
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
4
by: Rahul | last post by:
Hi Everyone, It is well known that the input parameter which is passed to the copy constructor is passed as reference and not as as object. Because passing an object is as good as making another...
15
by: subramanian100in | last post by:
consider the following program: #include <iostream> using namespace std; class Test { public: Test(int xx) : x(xx) { cout << x << endl; }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.