mardi 26 juillet 2016

C++ const vs non-const resolution affected by 'private'

Why does private vs public status affect overloaded function resolution?

class T;

class U {
   public:
    void g();
    const T* f() const;
   private:
    T* f();
}

U::g() { ... ; T* x = f(); ... } // invalid conversion from const T* to T*

does not compile (g++ 5.4, std=c++11). The error disappears if T* f(); is moved from private to public.

Assuming that the compiler correctly implements the standard, my question is: what rule is causing this behavior, and what is the rationale for this rule?

Aucun commentaire:

Enregistrer un commentaire