Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator in namespace scope hiding another in global scope

Is this a compiler-bug?

template <typename T>
T& operator++(T& t)
{
    return t;
}

namespace asdf {

enum Foo { };
enum Bar { };

Foo& operator++(Foo& foo);

void fun()
{
    Bar bar;
    ++bar;
}

} // end namespace asdf

int main()
{
    return 0;
}

The GCC 4.7 error message is:

error: no match for 'operator++' in '++bar'
note: candidate is:
note: asdf::Foo& asdf::operator++(asdf::Foo&)
note: no known conversion for argument 1 from 'asdf::Bar' to 'asdf::Foo&'

It compiles if you comment out the line:

Foo& operator++(Foo& foo);
like image 531
TommiT Avatar asked Jan 16 '13 08:01

TommiT


People also ask

What is global namespace scope?

Global scope or global namespace scope is the outermost namespace scope of a program, in which objects, functions, types and templates can be defined. A name has global namespace scope if the identifier's declaration appears outside of all blocks, namespaces, and classes.

What is the :: operator C++?

The :: (scope resolution) operator is used to qualify hidden names so that you can still use them.

What is the function of scope resolution operator?

The scope resolution operator :: is used to identify and disambiguate identifiers used in different scopes. For more information about scope, see Scope.

How do we access a variable defined inside of a namespace?

1. Using Scope Resolution Operator (::) The scope resolution operator ( :: ) can be used with the name of the namespace to call/access any member (variable, function, or class) declared inside a namespace.


3 Answers

No that is not a bug. There are three parallel sets of operators considered. Members, non-member operators, and builtins.

The non-member ones are looked up by normal unqualified+ADL lookup, ignoring all class member functions. Hence the global operator is hidden by a lexical more closer one (and an intervening member function wouldn't have hidden other non-members).

Note that overload resolution takes place after name lookup1; in your case the name operator++ was found, but no appropriate overload.

If Bar had been declared globally, and/or the other operator in namespace asdf, ADL (in the former case) or ordinary unqualified lookup (in the latter case) would have dragged the operator in.


1: Overload resolution (...) takes place after name lookup has succeeded. (C++ Standard)

like image 114
Johannes Schaub - litb Avatar answered Nov 01 '22 14:11

Johannes Schaub - litb


No, this is not a compiler bug.

There are two name-lookups that get performed for the expression ++bar.

  • The regular name lookup searches the enclosing scopes and namespaces until it finds the first occurence of operator++. This search works inside out, so the global namespace is searched last. When looking for operator functions, member-functions are treated separately (and don't stop this search).
  • The argument-dependent lookup kicks in next and searches additional classes and namespaces, but only those that are related to the arguments of the function (operator++ in this case).

In the example in the question, the normal lookup finds asdf::operator++ and stops looking.
The argument-dependent lookup only adds the asdf namespace to the places to search, because that is the associated namespace for enum Bar. For that reason, the global operator++ can not be found.

You can make the global operator++ be found with a using declaration in namespace asdf.

like image 35
Bart van Ingen Schenau Avatar answered Nov 01 '22 15:11

Bart van Ingen Schenau


Overloading only applies to names defined in the same scope. Once the compiler finds a matching name it doesn't look in outer scopes, even if the name it found applies to something that can't be used. This has nothing to do with operators; if the code used a function name in the same way that it uses operator++ it would get the same error. For example:

void f(int);

struct C {
void f(const C&);
void g() {
    f(3); // error: f(const C&) can't be called with argument 3
};
like image 1
Pete Becker Avatar answered Nov 01 '22 15:11

Pete Becker