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<VectorVi ew<Ref> >
{
Ref r;
public:
VectorView() {}
operator const Ref &() const { return r; }
operator Ref &() { return r; }
};
template<class T>
class DenseVector: public Vector<DenseVec tor<T> >
{
public:
DenseVector() {}
DenseVector(con st DenseVector<T> &rhs) {}
VectorView<Dens eVector<T> >
operator()(int from, int to)
{ return VectorView<Dens eVector<T> >(); }
};
int fun(const DenseVector<dou ble> &x) { return 1; }
int main()
{
DenseVector<dou ble> x, y;
DenseVector<dou ble> &w = x(1,3); // (1)
DenseVector<dou ble> 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 5 1887
Alexander Stippler wrote: int main() { DenseVector<dou ble> x, y; DenseVector<dou ble> &w = x(1,3); // (1) DenseVector<dou ble> 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
In <43************ ***********@aut hen.white.readf reenews.net> Valentin
Samko wrote: Alexander Stippler wrote: int main() { DenseVector<dou ble> x, y; DenseVector<dou ble> &w = x(1,3); // (1) DenseVector<dou ble> 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
Alexander Stippler wrote: DenseVector<dou ble> x, y; DenseVector<dou ble> &w = x(1,3); // (1) DenseVector<dou ble> 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
In <43************ ***********@aut hen.white.readf reenews.net> Valentin
Samko wrote: Alexander Stippler wrote: DenseVector<dou ble> x, y; DenseVector<dou ble> &w = x(1,3); // (1) DenseVector<dou ble> 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<dou ble> &x), so
the argument is a const &. And the copy constructor for (2) expects
a const & too. Where am I wrong?
Alexander Stippler wrote: > DenseVector<dou ble> x, y; > DenseVector<dou ble> &w = x(1,3); // (1) > DenseVector<dou ble> 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<dou ble> &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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 function func(const String ¶m). When an actual
String is passed as the param this is nice and efficient.
I have a constructor which takes a const char* as an argument. And
|
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 returned (using copy constructor)
}
int main()
|
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(){
|
by: David Lindauer |
last post by:
If I do this:
struct aa
{
int r,s,t;
aa();
aa(const aa& t);
};
int tt()
|
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) {}
complex( const complex& c) :re(c.re), im(c.im) {}
| |
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(). Hence (?) m is created as copy of
temp_m. But the copy constructor is not called. Contradiction. Where am
I thinking incorrectly?
|
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 creation of this
implicit default constructor, to force the creation of a struct via my
constructor only?
Zytan
|
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 copy which in itself
needs a copy constructor.
However i was wondering why can't the existing object be passed as
a pointer instead of a reference to the copy constructor which creates
a new object.
|
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; }
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |