473,385 Members | 1,409 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,385 software developers and data experts.

Pitfall of proxy instead of temporary?


There are two common ways of implementing the postincrement operator:

(1) Copy-construct a temporary.
Increment the original.
Return the tempory.

(2) Create an "proxy" object which, upon its destruction, will increment
the original object.
Return the original object.

It appears to me that method (2) might act strangely in the following
circumstance:

x = x++, x++, x;

There's a sequence point between the comma operands, but any temporaries
aren't destroyed until the semi-colon is reached.

--

Frederick Gotham
Aug 30 '06 #1
8 1617
On Wed, 30 Aug 2006 18:36:39 GMT, Frederick Gotham
<fg*******@SPAM.comwrote:
>There are two common ways of implementing the postincrement operator:
(2) Create an "proxy" object which, upon its destruction, will increment
the original object. Return the original object.
That way is probably used for very special cases only. Any example?
Aug 30 '06 #2
Frederick Gotham wrote:
There are two common ways of implementing the postincrement operator:

(1) Copy-construct a temporary.
Increment the original.
Return the tempory.

(2) Create an "proxy" object which, upon its destruction, will increment
the original object.
Return the original object.

It appears to me that method (2) might act strangely in the following
circumstance:

x = x++, x++, x;

There's a sequence point between the comma operands, but any temporaries
aren't destroyed until the semi-colon is reached.
If by 2 you mean something like:
X X::operator++(int)
{
Proxy p(*this);
return *this;
}

then there is no problem, because 'p' is destroyed before the
sequence point associated with the function returning.

Aug 31 '06 #3
Old Wolf posted:
If by 2 you mean something like:
X X::operator++(int)
{
Proxy p(*this);
return *this;
}

then there is no problem, because 'p' is destroyed before the
sequence point associated with the function returning.

The object, "p", is destroyed before the object is returned by reference, so
the function would act like a preincrement rather than a postincrement.

(I can't remember exactly how the proxy thing actually worked...)

--

Frederick Gotham
Aug 31 '06 #4
Frederick Gotham wrote:
Old Wolf posted:
>If by 2 you mean something like:
X X::operator++(int)
{
Proxy p(*this);
return *this;
}

then there is no problem, because 'p' is destroyed before the
sequence point associated with the function returning.


The object, "p", is destroyed before the object is returned by reference,
so the function would act like a preincrement rather than a postincrement.

(I can't remember exactly how the proxy thing actually worked...)
Maybe, you recall something like this:

struct post_inc {

struct post_inc_proxy {

post_inc & i;

post_inc_proxy ( post_inc & r )
: i ( r )
{}

~post_inc_proxy ( void ) {
++i.i;
}

operator post_inc const & ( void ) {
return i;
}

}; // post_inc_proxy;

int i;

post_inc ( int v )
: i ( v )
{}

post_inc_proxy operator++ ( int ) {
return ( *this );
}

}; // post_inc
#include <iostream>

int main ( void ) {
{
post_inc a ( 5 );
post_inc b ( 20 );
post_inc c ( 20 );
b = a++, c = a++, a++;
std::cout << a.i
<< '\n'
<< b.i
<< '\n'
<< c.i
<< '\n';
}
{
int a ( 5 );
int b ( 20 );
int c ( 20 );
b = a++, c = a++, a++;
std::cout << a
<< '\n'
<< b
<< '\n'
<< c
<< '\n';
}
}

And, yes: it fails with the comma operator.
Best

Kai-Uwe Bux
Aug 31 '06 #5
Kai-Uwe Bux posted:
And, yes: it fails with the comma operator.

To be honest, I wouldn't even bother defining a postincrement operator for
my class -- it's too messy. If they really want to write:

x = obj++;

, then let them write:

x = obj;
++obj;

On a semi-unrelated topic:
Is an implementation of C++ allowed to change the following:

for(T i = 0; i != MAX; i++)

into:

for(T i = 0; i != MAX; ++i)

That is to say, is it allowed to change a postincrement to a preincrement
_if_ the result is discarded. There's still a lot of programmers out there
who use i++ where ++i would suffice.

--

Frederick Gotham
Aug 31 '06 #6
Frederick Gotham wrote:
Kai-Uwe Bux posted:
>And, yes: it fails with the comma operator.


To be honest, I wouldn't even bother defining a postincrement operator for
my class -- it's too messy. If they really want to write:

x = obj++;

, then let them write:

x = obj;
++obj;
The standard idiom

T operator++ ( int ) {
T dummy ( *this );
++ (*this);
return ( dummy );
}

is not messy at all. Using return value optimization and inlinening it
should compile to virtually the same object code.

On a semi-unrelated topic:
Is an implementation of C++ allowed to change the following:

for(T i = 0; i != MAX; i++)

into:

for(T i = 0; i != MAX; ++i)

That is to say, is it allowed to change a postincrement to a preincrement
_if_ the result is discarded.
That depends on the type T. If the observable behavior is the same, then:
yes, the implementation is allowed to make the change. However, for
user-defined types, the compiler would have to prove that the effect of
postfix++ and prefix++ on objects of type T is the same: I do not know of a
provision in the standard that in a well-formed program postfix and prefix
operators have semantics that are related in the usual way.
There's still a lot of programmers out there who use i++ where ++i would
suffice.

Best

Kai-Uwe Bux
Aug 31 '06 #7
Kai-Uwe Bux posted:
The standard idiom

T operator++ ( int ) {
T dummy ( *this );
++ (*this);
return ( dummy );
}

is not messy at all. Using return value optimization and inlinening it
should compile to virtually the same object code.

But then there's the small issue of an accessible copy-constructor...

That depends on the type T. If the observable behavior is the same, then:
yes, the implementation is allowed to make the change. However, for
user-defined types, the compiler would have to prove that the effect of
postfix++ and prefix++ on objects of type T is the same: I do not know of
a
provision in the standard that in a well-formed program postfix and
prefix
operators have semantics that are related in the usual way.

Maybe it would be a good idea to allow postincrement to become preincrement
when the value is discarded? Or then again, maybe we should just drill into
people's head that they should be using ++i instead of i++.

--

Frederick Gotham
Aug 31 '06 #8
Frederick Gotham wrote:
Kai-Uwe Bux posted:

>>The standard idiom

T operator++ ( int ) {
T dummy ( *this );
++ (*this);
return ( dummy );
}

is not messy at all. Using return value optimization and inlinening it
should compile to virtually the same object code.

But then there's the small issue of an accessible copy-constructor...
Iterators are required to be copy constructible.
Aug 31 '06 #9

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

Similar topics

0
by: Jakub Nowak | last post by:
Hi! Can I and how use temporary certificate as proxy to establish connection with LDAP server? Instead of using ldap_bind() to authentication I want to use alternative function to...
4
by: Fuzzyman | last post by:
In a nutshell - the question I'm asking is, how do I make a socket conenction go via a proxy server ? All our internet traffic has to go through a proxy-server at location 'dav-serv:8080' and I...
2
by: nitrogenycs | last post by:
Hello, I need a way to get a notification whenever a variable of an object changes. The approach should be non-intrusive so that I can use existing objects without modifying them. I want to be...
5
by: Benne Smith | last post by:
Hi, I have three enviroments; a development, a testing and a production enviroment. I'm making a big application (.exe), which uses alot of different webservices. I don't use the webservices...
4
by: WATYF1 | last post by:
Hello. I'm writing a VB.NET app to check email message counts for both POP3 and IMAP4. I'm using TCPClient to connect, and a NetworkStream to send simple commands. It's a very simple bit of code,...
6
by: sameer | last post by:
..NET Framework 1.1 VS2003 Application is making webservice calls from behind a proxy server and then freezes and dies. Questoin is can i use webservice over proxy server( i guess another name...
7
by: Pro1712 | last post by:
Hello, I need to write a simple proxy server. What I want to do is to use HttpListener to get requests from the browser, add some proxy information and some other stuff and send the request to...
23
by: Kira Yamato | last post by:
It is erroneous to think that const objects will have constant behaviors too. Consider the following snip of code: class Person { public: Person(); string get_name() const
10
by: raylopez99 | last post by:
Beware this newbie trap: private bool myFunction (Myenum X) { bool local_bool; //this will not compile; you need to do this first: bool local_bool = true; switch (X) { case 1:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.