Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespaces and operator resolution

I am using a library that defines output stream operators (operator<<) in the global namespace. In my own namespace, I was always declaring such operators in the global namespace, and never had problems with them. But now for various reasons I need to declare those operators in my own namespace, and suddenly, the compiler can't seem to find the operators declared in the library anymore.

Here's a simple example that illustrates my problem:

#include <iostream>  namespace A {    struct MyClass {}; }  std::ostream & operator<<( std::ostream & os, const A::MyClass & )    { os << "namespace A"; return os; }  namespace B {    struct MyClass {};     std::ostream & operator<<( std::ostream & os, const B::MyClass & )       { os << "namespace B"; return os; } }  namespace B {    void Test()    {       std::cout << A::MyClass() << std::endl;       std::cout << B::MyClass() << std::endl;    } }  int main() {    B::Test();    return 1; } 

I am getting the following error:

error: no match for ‘operator<<’ in ‘std::cout << A::MyClass()’ 

Note that if both operators are within the namespaces, or alternatively if they are both in the global namespace, the code compiles and executes correctly.

I'd really like to understand what's going on and also what the "good practice" for defining such operators with namespaces.

Thanks!

like image 658
chataign Avatar asked Mar 04 '11 15:03

chataign


People also ask

What is a namespace of the operator?

Overview. A namespace-scoped operator watches and manages resources in a single Namespace, whereas a cluster-scoped operator watches and manages resources cluster-wide. An operator should be cluster-scoped if it watches resources that can be created in any Namespace.

What is the resolution operator used for?

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

What is scope resolution operator explain?

The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class.

What is scope resolution operator Ruby?

find_by("123abc") is called the Scope Resolution Operator. You can think about it like this. The thing on the right-hand side of the :: operator resolves to the scope of the thing on the left-hand side. In the case of something like Net::HTTP , the HTTP resolves to being scoped to the Net namespace.


1 Answers

Since Test is within namespace B the compile sees the operator in that namespace and notes that it doesn't have a matching signature. It also attempts to find the operator in namespace A which contains the class but can't find it there either. Because there's already such an operator (with the wrong signature) in namespace B it won't go and attempt to find one at global scope.

The reason it doesn't search for the global one is roughly as follows. I'm first going to quote the standard and then try to explain it.

From 3.4/1:

...Name lookup may associate more than one declaration with a name if it finds the name to be a function name; the declarations are said to form a set of overloaded functions (13.1). Overload resolution (13.3) takes place after name lookup has succeeded.

As I read this, when the compiler is trying to find a function (which your operators is in this context) it first tries to do name lookup to find the function first. Then next it tries to pick the right function from the set of overloads.

Now from 3.4.1/6:

A name used in the definition of a function(26) that is a member of namespace N (where, only for the purpose of exposition, N could represent the global scope) shall be declared before its use in the block in which it is used or in one of its enclosing blocks (6.3) or, shall be declared before its use in namespace N or, if N is a nested namespace, shall be declared before its use in one of N’s enclosing namespaces.

Let's break this down. You're using the operator<< in a namespace level function so this section applies. It's going to try to find that operator using the precedence in the described above. Your operator isn't declared in the current block or enclosing blocks (this is referring to nested {} within your function). However the next part matches "...shall be declared before its use in namespace N...". There is in fact an operator<< in the current namespace (B) so it adds that operator to its list of matches. There aren't any more matches in B, and because same-namespace scope is considered the best possible match-closeness, it won't look into any other scopes.

The reason it works when you put the operator into namespace A is that since the item being printed is a member of A, that namespace is actually considered because it's included in the namespaces of the expression. Since namespace A is considered it finds the appropriate match in that namespace and compiles correctly.

Now that it has a list of possible operators, it tries to do overload resolution on them. Unfortunately the one in found in namespace B is the only one it considers and it doesn't match the required arguments.

In general you should have the insertion operators in the same namespace as the class upon which it operates.

like image 150
Mark B Avatar answered Sep 22 '22 05:09

Mark B