prefix decrement on temporary object

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

Consider the code fragment:

vector<int> container;

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
 
S

Salt_Peter

Consider the code fragment:

vector<int> container;

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<int> container;
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().
 
J

James Kanze

Consider the code fragment:
vector<int> container;
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.
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top