[patch] Remove is_merge_section_for

Cary Coutant ccoutant@google.com
Mon Mar 23 17:14:00 GMT 2015


>> +  const Input_merge_map*
>> +  get_input_merge_map(unsigned int shndx) const {
>> +    return const_cast<Object_merge_map*>(this)->get_input_merge_map(shndx);
>> +  }
>>
>> When you need both const and non-const versions of a method, have the
>> non-const version call the const version, instead of the other way
>> around.
>
> I did as asked, but now we need two casts:
>
>  Input_merge_map *
>   get_input_merge_map(unsigned int shndx) {
>     return const_cast<Input_merge_map *>(static_cast<const Object_merge_map *>(
>                                              this)->get_input_merge_map(shndx));
>   }
>
> Are you sure this is better?

Yes. The other way, by casting away the const-ness before you call the
non-const method, you risk that the non-const method might modify the
object that we only had a const pointer to. This way, we know it's
safe to cast away the const-ness because we were given a non-const
pointer to begin with.

-cary



More information about the Binutils mailing list