 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Angel Tsankov Guest
|
Posted: Sun Feb 26, 2006 11:06 pm Post subject: pre/post increment/decrement return type? |
|
|
Should pre/post increment/decrement return const or non-const?
What about other functions? |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Sun Feb 26, 2006 11:06 pm Post subject: Re: pre/post increment/decrement return type? |
|
|
Angel Tsankov wrote:
| Quote: | Should pre/post increment/decrement return const or non-const?
|
Non-const, otherwise the expression can't be an lvalue.
| Quote: | What about other functions?
|
That depends what they return. These is no fixed rule.
--
Ian Collins. |
|
| Back to top |
|
 |
Angel Tsankov Guest
|
Posted: Sun Feb 26, 2006 11:06 pm Post subject: Re: pre/post increment/decrement return type? |
|
|
"Ian Collins" <ian-news (AT) hotmail (DOT) com> wrote in message news:1140993572.791376@drone2-svc-skyt.qsi.net.nz...
| Quote: | 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 |
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Mon Feb 27, 2006 12:06 am Post subject: Re: pre/post increment/decrement return type? |
|
|
Ian Collins wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Mon Feb 27, 2006 12:06 am Post subject: Re: pre/post increment/decrement return type? |
|
|
Kai-Uwe Bux wrote:
| Quote: | 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. |
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Mon Feb 27, 2006 12:06 am Post subject: Re: pre/post increment/decrement return type? |
|
|
Angel Tsankov wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
benben Guest
|
Posted: Mon Feb 27, 2006 1:06 am Post subject: Re: pre/post increment/decrement return type? |
|
|
| Quote: | 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 |
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Mon Feb 27, 2006 1:06 am Post subject: Re: pre/post increment/decrement return type? |
|
|
Ian Collins wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Mon Feb 27, 2006 2:06 am Post subject: Re: pre/post increment/decrement return type? |
|
|
Kai-Uwe Bux wrote:
| Quote: |
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. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|