Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A compiling example of the hiding mentioned in the Note in [over.unary]/2

[over.unary]/2

The unary and binary forms of the same operator are considered to have the same name. [ Note: Consequently, a unary operator can hide a binary operator from an enclosing scope, and vice versa. —end note ]

I'd like to see a compiling example of a snippet where this hiding occurs.

like image 342
João Afonso Avatar asked Feb 04 '23 00:02

João Afonso


1 Answers

A fairly simple example1:

struct foo {
    void operator+(foo const&) {}
};

struct bar : foo {
    void operator+() {}
};

int main() {
    bar a, b;
    a + b; // Can't add two bars
}

The name of the member function is operator+, so the one declared in bar hides the one in foo when we overload it. That makes the addition at the end of main ill-formed.

But if you had two foo objects (that are not bar), the addition would be perfectly okay.


1 - Pardon me it's a non-compiling one, but usually the issue with name hiding is that it prevents programs from building all of a sudden.

like image 153
StoryTeller - Unslander Monica Avatar answered May 11 '23 01:05

StoryTeller - Unslander Monica