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

pre/post increment/decrement return type?

Should pre/post increment/decrement return const or non-const?
What about other functions?
Feb 26 '06 #1
8 3171
Angel Tsankov wrote:
Should pre/post increment/decrement return const or non-const?
Non-const, otherwise the expression can't be an lvalue.
What about other functions?


That depends what they return. These is no fixed rule.

--
Ian Collins.
Feb 26 '06 #2

"Ian Collins" <ia******@hotmail.com> wrote in message news:11***************@drone2-svc-skyt.qsi.net.nz...
Angel Tsankov wrote:
Should pre/post increment/decrement return const or non-const?


Non-const, otherwise the expression can't be an lvalue.


Why should it be an lvalue?

int i = 0;
i++; // returns rvalue
Feb 26 '06 #3
Angel Tsankov wrote:
Should pre/post increment/decrement return const or non-const?
What about other functions?


Totally up to you. While the behaviour of pre- and post-increment
is somewhat expected, based on the behaviour of the built-in types,
'other functions' are certaintly not. And even then, if you are
the one who designs and implements pre- and post-increments for
your UDTs, you're free to make them do what you please. For all
we care, they can all return 'void'. Do as your model dictates.
There is no good enough general rule.

V
--
Please remove capital As from my address when replying by mail
Feb 26 '06 #4
Ian Collins wrote:
Angel Tsankov wrote:
Should pre/post increment/decrement return const or non-const?


Non-const, otherwise the expression can't be an lvalue.


Why would you want, e.g., iter++ to be an lvalue? Also, the standard
specified the following requirements for iterators:

++r returns &X (X being the type of the iterator)
r++ returns something convertible to X const &

[snip]
Best

Kai-Uwe Bux

Feb 26 '06 #5
Kai-Uwe Bux wrote:
Ian Collins wrote:

Angel Tsankov wrote:
Should pre/post increment/decrement return const or non-const?


Non-const, otherwise the expression can't be an lvalue.

Why would you want, e.g., iter++ to be an lvalue? Also, the standard
specified the following requirements for iterators:

You're right, I probably wouldn't, but ++iter could be if you wanted to
assign something to the result,

*(++i) = x;

where i is some type with const and non-const operator*().

Then again, if iter were some form of smart pointer, one might want to write

*(i++) = x;

because it's valid for normal pointers. Make sense, or am I lacking
caffeine?

--
Ian Collins.
Feb 26 '06 #6
Ian Collins wrote:
Kai-Uwe Bux wrote:
Ian Collins wrote:

Angel Tsankov wrote:

Should pre/post increment/decrement return const or non-const?

Non-const, otherwise the expression can't be an lvalue.

Why would you want, e.g., iter++ to be an lvalue? Also, the standard
specified the following requirements for iterators:

You're right, I probably wouldn't, but ++iter could be if you wanted to
assign something to the result,

*(++i) = x;

where i is some type with const and non-const operator*().

Then again, if iter were some form of smart pointer, one might want to
write

*(i++) = x;

because it's valid for normal pointers. Make sense, or am I lacking
caffeine?


Maybe, I am missing something, but isn't that confusing:

T const * <--> const_iterator

and:

T * const <--> iterator const
The following compiles:
class iter_dummy {

int* ptr;

public:

iter_dummy ( int i )
: ptr ( new int ( i ) )
{}

~iter_dummy ( void ) {
delete ptr;
}
iter_dummy const & operator++ ( int ) {
return ( *this );
}

int & operator* ( void ) const {
return ( *ptr );
}

}; // iter_dummy
int main ( void ) {
iter_dummy iter ( 4 );
*(iter++) = 5;
}
Note how operator*() is const as it does not change the iterator but only
the pointee. Thus, you can have iter++ return a const reference, yet
*(iter++) be and lvalue.

Also, note that the standard, e.g., in the description of
std::reverse_iterator, sets a corresponding precedence: the signature of
the dereferencing operator is:

reference operator*() const;

Best

Kai-Uwe Bux
Feb 27 '06 #7
> Totally up to you. While the behaviour of pre- and post-increment
is somewhat expected, based on the behaviour of the built-in types,
'other functions' are certaintly not. And even then, if you are
the one who designs and implements pre- and post-increments for
your UDTs, you're free to make them do what you please. For all
we care, they can all return 'void'. Do as your model dictates.
There is no good enough general rule.

However, if you want your overloaded operators to behave just like those
of built-in types then you should consider making the return type non-const.

Operators like ++ typically returns a self-reference and are themselves
hardly a const operation. In order to use the operator the user must
have a non-const reference to the object so it is reasonable to assume
the user already being in power of legally modifying the object, which
makes returning a const reference from the operator less convincing.

Regards,
Ben
Feb 27 '06 #8
Kai-Uwe Bux wrote:

int & operator* ( void ) const {
return ( *ptr );
}

}; // iter_dummy
int main ( void ) {
iter_dummy iter ( 4 );
*(iter++) = 5;
}
Note how operator*() is const as it does not change the iterator but only
the pointee. Thus, you can have iter++ return a const reference, yet
*(iter++) be and lvalue.

Good point, I was a little short on caffeine.

--
Ian Collins.
Feb 27 '06 #9

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

Similar topics

9
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...
8
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 // //...
5
by: Ian Pilcher | last post by:
I'm trying to figure out if an increment to a variable of an integer type, followed by a decrement, (or vice versa) is guaranteed to restore the variable to its initial value, even if the first...
18
by: Patrick Wood | last post by:
I found a problem with C# and post increments. I was going through some source code in c++ and found someone did a post increment: int x=0; for(int i=0; i<10; i++) { x = x++; }
2
by: Michael B. | last post by:
Hi! I have a very simple question: Is it still true that pre variable incrementation (like ++i) is faster than post incrementation (i++). I heard that in modern compilers it makes no difference...
5
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...
11
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
13
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are...
3
by: Stang1 | last post by:
The following statement: line_buf = ' '; is equivalent to: line_buf = ' '; line_len++;
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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
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,...
0
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...
0
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,...
0
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...

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.