4

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.

Zhro
  • 2,546
  • 2
  • 29
  • 39
  • I didn't see any obvious errors and this compiles and runs fine for me in visual studio. I'll wait for someone more familiar with G++ answer this. – Goodies Feb 11 '16 at 23:39
  • Although this seems to be relevant http://stackoverflow.com/questions/642229/why-do-i-need-to-use-typedef-typename-in-g-but-not-vs – Goodies Feb 11 '16 at 23:40

2 Answers2

11

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

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
  • Upvoted for the standard quote. Didn't know that it is legal. There is an old (2008) discussion on comp.lang.c++.moderated about exactly this issue: http://compgroups.net/comp.lang.c++.moderated/const-reference-typedef-compile-error/257420 But couldn't find (yet) a gcc bug report. – vsoftco Feb 11 '16 at 23:49
  • Oddly enough, `const A::reference` works. Presumably the utterly pointless `typename` caused a different code path to be triggered. – T.C. Feb 12 '16 at 01:10
3

You cannot apply const to a reference, like const(ref(type)).

But you can have a reference to a const type, like ref(const(type)).

BitWhistler
  • 1,439
  • 8
  • 12