473,508 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

prefix decrement on temporary object

Consider the code fragment:

vector<intcontainer;

container.insert(container.begin(), 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 2025
On Jan 7, 8:26 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Consider the code fragment:

vector<intcontainer;

container.insert(container.begin(), 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<intcontainer;
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.comwrote:
Consider the code fragment:
vector<intcontainer;
container.insert(container.begin(), 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 objektorientierter Datenverarbeitung
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.comwrote:
Consider the code fragment:
vector<intcontainer;
container.insert(container.begin(), 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 objektorientierter Datenverarbeitung
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
3175
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...
5
1681
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...
6
2235
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...
8
6238
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 // //...
98
14310
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
13270
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...
8
7822
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
2486
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...
3
3792
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...
0
7231
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,...
0
7133
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...
0
7405
jinu1996
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...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
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...
0
4724
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...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
435
bsmnconsultancy
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...

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.