473,586 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

prefix decrement on temporary object

Consider the code fragment:

vector<intconta iner;

container.inser t(container.beg in(), 10);

int& ref = *--container.end() ;

From this, it looks like we can apply prefix decrement operator to
container.end() - ie 'prefix --' can be applied to the iterator type
object which happens to be a temporary here.

In general can we apply prefix/postfix increment/decrement operator on
a temporary object of some class type ?

Kindly clarify.

Thanks
V.Subramanian

Jan 7 '08 #1
3 2032
On Jan 7, 8:26 am, "subramanian10. ..@yahoo.com, India"
<subramanian10. ..@yahoo.comwro te:
Consider the code fragment:

vector<intconta iner;

container.inser t(container.beg in(), 10);

int& ref = *--container.end() ;

From this, it looks like we can apply prefix decrement operator to
container.end() - ie 'prefix --' can be applied to the iterator type
object which happens to be a temporary here.

In general can we apply prefix/postfix increment/decrement operator on
a temporary object of some class type ?

Kindly clarify.

Thanks
V.Subramanian

You can increment/decrement a temporary all you like, what is being
stored here is the resulting reference to an element. Some
implementations of std::vector use pointers as iterators, with
pointers - prefix decrement/increment may fail under certain
conditions (ie: passing the resulting temporary to a function). If
your vector implementation uses a iterator type instead of a pointer,
you'll be fine. Nonetheless, --container.end() is considered to be non-
portable.

Which then begs the question - why not use:

int& ref = container.back( );

and since you appear to be pushing elements at front of vector, why
aren't you choosing a std::deque< int instead?

#include <iostream>
#include <deque>

int main()
{
std::deque<intc ontainer;
container.push_ front(10);
container.push_ front(9);
container.push_ back(11);
int& ref = container.back( );
std::cout << ref << std::endl;
}

Note: a deque does not store its elements in contiguous storage like
an array or vector. It has random iterators and op[] as well as at().

Jan 7 '08 #2
On Jan 7, 2:26 pm, "subramanian10. ..@yahoo.com, India"
<subramanian10. ..@yahoo.comwro te:
Consider the code fragment:
vector<intconta iner;
container.inser t(container.beg in(), 10);
int& ref = *--container.end() ;
From this, it looks like we can apply prefix decrement
operator to container.end() - ie 'prefix --' can be applied to
the iterator type object which happens to be a temporary here.
It's unspecified.
In general can we apply prefix/postfix increment/decrement
operator on a temporary object of some class type ?
The built-in operator -- requires an lvalue, and will not work
on a temporary object. A user defined operator -- may or may
not require an lvalue, depending on whether it is a member
function or a free function. The standard does not specify
whether the -- above is a user defined operator or the built-in
operator, and for user defined operators, it leaves the
implementation free to choose whether it uses a member function
or a free function to overload the operator.

So your expression might work, or it might not.

--
James Kanze (GABI Software) mailto:ja****** ***@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
Jan 7 '08 #3
On Jan 7, 3:07 pm, Salt_Peter <pj_h...@yahoo. comwrote:
On Jan 7, 8:26 am, "subramanian10. ..@yahoo.com, India"
<subramanian10. ..@yahoo.comwro te:
Consider the code fragment:
vector<intconta iner;
container.inser t(container.beg in(), 10);
int& ref = *--container.end() ;
From this, it looks like we can apply prefix decrement
operator to container.end() - ie 'prefix --' can be applied
to the iterator type object which happens to be a temporary
here.
In general can we apply prefix/postfix increment/decrement
operator on a temporary object of some class type ?
You can increment/decrement a temporary all you like,
No. You cannot increment or decrement a temporary. You can
call a member function on a temporary, however---even a
non-const member function. In his case, operator-- is probably
a member function, which is why the code compiles.
what is being stored here is the resulting reference to an
element. Some implementations of std::vector use pointers as
iterators, with pointers - prefix decrement/increment may fail
under certain conditions (ie: passing the resulting temporary
to a function).
Trying to increment a temporary pointer will always fail,
regardless of the conditions.
If your vector implementation uses a iterator type instead of
a pointer, you'll be fine.
That's not guaranteed either. For a class type, operator-- can
be either a member or a non-member function. If it's a
non-member, the parameter is almost certainly a non-const
reference, and you can't bind a temporary to a non-const
reference.
Nonetheless, --container.end() is considered to be non-
portable.
Because it's not guaranteed to work.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 8 '08 #4

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

Similar topics

9
3183
by: Mark Turney | last post by:
I was reading "Practical C++ Programming" yesterday, and it mentioned that the order of execution for post-increment and post-decrement operators was ambiguous. I had previously learned that a post-increment or post-decrement operator modifies the operand once the entire statement has been executed, not during execution of the statement, so...
5
1685
by: White Wolf | last post by:
Hi, I would like to double check how long a temporary returned by a function lives? Suppose I have an instance of a class type C, which has a member function returning some sort of wrapper-decorator by value - thereby creating an unnamed temporary. Assuming I start using the temporary (by calling its members) in the same expression, can...
6
2241
by: Sergey | last post by:
Hello! Could anybody be kind enough to explain this concept? Why C++ make two ops for prefix and postfix ++ operator? As I guess, it is possible to implement both cases via sole prefix increment operation. Correct me if I'm wrong, Let's see trival example:
8
6255
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // // >g++ test.cpp // test.cpp: In function `int main()':
98
14380
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
5
13282
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that will increment/decrement the form field value by 1 when clicked. I'm using the following code, however when I click on one of the links, I get the...
8
7826
by: subramanian100in | last post by:
Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference
2
2498
by: swapnaoe | last post by:
Hi, In http://elearning.embnet.org/file.php/43/ctutorial/Postfix-and-prefix----and---.html I read about postfix and prefix unary operators. It said " If the increment or decrement operator is used as a prefix, the operation is performed before the function call. If the operator is used as a postfix, the operation is performed after the...
3
3802
by: news.aioe.org | last post by:
Is it possible to overload increment(++) and decrement(--) postfix and prefix operators for primitive datatypes such as int, char, short, etc. in global scope (vs as a class member function where this is possibe)? If there is a site that lists all operators that can't be overloaded (such as the member access dot operator), please post it. ...
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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...
0
8202
Oralloy
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. ...
0
8216
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5710
isladogs
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...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
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...
0
3865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
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 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.