const foo& - what is const - ReferenceType or ReferenceType::baseType ?

David Nolden david.nolden.kdevelop at art-master.de
Wed Mar 2 16:34:13 UTC 2011


2011/3/2 Milian Wolff <mail at milianw.de>:
> Hey David,
>
> just spotted an issue in the shortenedTypeIdentifier, it checks whether the
> identifier is a const ref using this code:
>
>  bool isReference = false;
>  bool isConstReference = false;
>  if(type.cast<ReferenceType>()) {
>    isReference = true;
>    isConstReference = type.cast<ReferenceType>()->modifiers() &
> AbstractType::ConstModifier;
>    type = type.cast<ReferenceType>()->baseType();
>  }
>
> But the reality shows: Not the ReferenceType is const, but it's baseType! So
> I'll fix it here, but I wonder whether this could not be hit elsewhere? Should
> the ReferenceType maybe return the same modifiers as it's baseType to prevent
> such problems?

This has changed with time. The way it is now, it is more logical,
because it is the same as with pointers. The difference is that a
reference cannot be constant at all in our sense, because references
are always constant, they cannot be changed to point to something
different after they were initialized. Well, maybe it would be good to
return the modifiers of the baseType in ReferenceType::modifiers,
because a ReferenceType cannot have modifiers at all. However this
would be slightly ugly, because it wouldn't be consistent with
"setModifiers", so we should probably keep everything as it is. Also
there might be other languages where reference-types _can_ take
const-modifiers.

Most places should handle this properly though. Would be nice if you
could add a unit-test to make sure this fix stays.

Greetings, David




More information about the KDevelop-devel mailing list