The return type of the function is not used when selecting overload. This is how the language works.
However, implicit conversions are selected based on context. Thus, technically you can compile it by returning something that is implicitly converted to both a link and a pointer.
#include <iostream> #include <string> struct Evil { std::string* p; Evil(std::string* p): p(p) {} operator std::string*() const { return p; } operator std::string&() const { return *p; } }; struct ConstEvil { const std::string* p; ConstEvil(const std::string* p): p(p) {} operator const std::string*() const { return p; } operator const std::string&() const { return *p; } }; class Foo { std::string s; public: ConstEvil GetString() const { return ConstEvil(&s); } Evil GetString() { return Evil(&s); } }; int main(int argc, char** argv){ Foo foo; const std::string& s = foo.GetString(); // ok return 0; }
But the real answer is that functions overloaded in constness should have a similar return type. (I suppose this means that the convention "uses pointers to mutable things, links to constant links" just breaks down.)
source share