On 1/7/2018 6:08 PM, Mr Flibble wrote:
> On 07/01/2018 11:16, Alf P. Steinbach wrote:
>> This is possibly a technique that others (e.g. you?) might find
>> useful, for avoiding redundancy for the case where a bunch of methods
>> should each have non-const and const version, but with the same code.
>>
<snip>
>
>
> Sorry Alf but your code is an over-engineered, overly complicated
> unreadable mess.
>
> This is all you need to do to simply and elegantly solve the problem:
>
> const foo& bar::baz() const
> {
> ...
> }
>
> foo& bar::baz()
> {
> return const_cast<foo&>(const_cast<const bar*>(this)->baz());
> }
>
Perhaps you thought that there exists cases where it can be a good idea
to do what you do here, hence it must always be the best idea on Earth,
with no need for anything else.
Essentially, with the casts you're giving up static type checking, which
of course can be fine -- and often is fine, that's why we have casts.
But it's not a general solution: in some cases your code's
const_cast<foo&> can lead to Undefined Behavior, namely when the foo is
original const (and that includes when it's dynamically allocated as
const) and the resulting reference is used. So you appear, at best, to
not have understood the purpose of a cast-free approach.
I'm sorry, but that means your evaluation is not worth shit.
Cheers!,
- Alf