12

What is the meaning of the highlighted sentence below? Does it have anything to do with function templates?

[over.load]/1:

Not all function declarations can be overloaded. Those that cannot be overloaded are specified here. A program is ill-formed if it contains two such non-overloadable declarations in the same scope. [ Note: This restriction applies to explicit declarations in a scope, and between such declarations and declarations made through a using-declaration ([namespace.udecl]). It does not apply to sets of functions fabricated as a result of name lookup (e.g., because of using-directives) or overload resolution (e.g., for operator functions). — end note ]

curiousguy
  • 8,038
  • 2
  • 40
  • 58
Alexander
  • 2,581
  • 11
  • 17

1 Answers1

11

You can do something like this:

namespace N {
  void f(int);
}

namespace M {
  int f(int);
}

using namespace N; // ok
using namespace M; // ok
// even if both have conflicting f's

You aren't directly overloading anything here. The using directives allow name lookup to find both functions and it's at that point that the call is ambiguous.

Here the sets of functions contain two non-overloadable are in there, but since they are found by name lookup as per the quote, they're okay.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
  • Note that the highlighted sentence in my question mentions _using-directives_, not _using-declarations_, which were used in your example. Nevertheless, I gave you the benefit of the doubt, in accepting your answer, given that the sentence says "**e.g.** because of _using-directives_" which could also be interpreted as "because of _using-declarations_". But now it dawned on me that a _using-directive_ only does lookup for type names, not for function names. (to be continued in the next comment) – Alexander Jul 16 '19 at 12:44
  • Therefore, either there is another interpretation for the sentence above, in which case your answer is incorrect, or the standard has a defect, i.e., the reference to _using-directives_ above is wrong and it should be replaced by _using-declarations_, in which case your answer is correct. Could you clarify? – Alexander Jul 16 '19 at 12:45
  • @Alexander "a *using-directive* only does lookup for type names, not for function names." No that is incorrect. You can imagine a using directive to be a using declaration for every declaration inside the namespace. You can use `using namespace N; using namespace M;` instead to get the same result. – Rakete1111 Jul 16 '19 at 12:50
  • Well I didn't express myself correctly when I wrote "a using-directive only does lookup for type names, not for function names.". But still, a using-directive doesn't do lookups for any names in the named namespace. – Alexander Jul 16 '19 at 12:57
  • 1
    @Alexander Well yeah, the declaration itself doesn't do any lookups itself, but name lookup uses it to look into the namespace. I don't understand your point, could you clarify. – Rakete1111 Jul 16 '19 at 13:01
  • This was extracted from your answer (emphasis is mine): "Here the sets of functions contain two non-overloadable are in there, but since they are found by **name lookup** as per the quote, they're okay." – Alexander Jul 16 '19 at 13:04
  • @Alexander I mean the name lookup that happens when you call the function. – Rakete1111 Jul 16 '19 at 13:08
  • But that's different. With a using-declaration, like in your example, you generate sets of function **fabricated** as a result of name lookups started in each using-declaration, That doesn't occur with a using-directive. – Alexander Jul 16 '19 at 13:12
  • @Alexander Yes. But those are not the same name lookups. You have one name lookup for each using declaration obviously to find out if the name actually exists. But then you have the set of functions found by name lookup when you call the function, and that is the set that the quote applies to. – Rakete1111 Jul 16 '19 at 16:16
  • As [this](http://coliru.stacked-crooked.com/a/62248c1bd77efa9c) compiles I have to agree with you. But [over.load]/1 is poorly written as it stands now. At least it should mention both, _using-declaration_ and _using-directives_ in the highlighted sentence. – Alexander Jul 16 '19 at 18:02
  • @Alexander Actually, now I'm not sure. I'll just change it to using directives so that we know it's right :) – Rakete1111 Jul 16 '19 at 18:08