Search code examples
c++constantsconst-correctness

Cannot apply const to typedef reference


The following code works when applying const to a return value reference of value_type& but errors if I use a typedef of the same type.

As an example:

class T {
};

class A {
public:
    typedef T value_type;
    typedef value_type& reference;

    // Not working
    const reference operator*() const;

    // But this works?
    //const value_type& operator*() const;
};

// Error!
const typename A::reference A::operator*() const {
}

int main() {
    return 0;
}

g++ will error with:

'const' qualifiers cannot be applied

My actual code uses templates but I've removed for the example and substituted class T instead. This has no bearing on the error.

I don't see why this won't work if specifying value_type& instead compiles fine.


Solution

  • There are two different issues here.

    First, in:

    typedef T* pointer;
    typedef const pointer const_pointer;
    

    the type of const_pointer is actually T* const, not const T*. The constness attaches to the pointer, not to the type pointed to.

    Now references obey the same logic: if you make a typedef for the reference type, and try to attach const to it, it will try to apply the const to the reference type, not the referenced type. But references, unlike pointers, are not allowed to have top-level cv-qualification. If you try to mention T& const, it's a compilation error. But if you try to attach the cv-qualification through a typedef, it's just ignored.

    Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a typedef-name (7.1.3, 14.1) or *decltype-specifier *(7.1.6.2), in which case the cv-qualifiers are ignored.

    ([dcl.ref]/1)

    So the second issue is that GCC thinks this is an error, whereas the standard clearly states that it should not be an error, it should just ignore the const. I think this is a bug in GCC. Clang does not produce an error: http://coliru.stacked-crooked.com/a/5b5c105941066708