For reasons I do not understand, the following C++ code fails to compile on VS 2022 (dialect set to C++20):
#include <compare>
namespace N1
{}
namespace N1::N2
{
class A {};
A operator-(A&);
}
std::strong_ordering operator-(std::strong_ordering o);
namespace N1
{
using namespace N2; // (1) !!!
std::strong_ordering foo();
inline std::strong_ordering bar()
{
return -foo(); // (2) !!!
}
}
At (2), the compiler files a complaint: error C2678: binary '-': no operator found which takes a left-hand operand of type 'std::strong_ordering' (or there is no acceptable conversion).
When the using namespace directive at (1) is removed, the compiler happily finds the operator- defined at global scope for the std::strong_ordering type.
This gives rise to a set of questions:
- Is this VS 2022 behavior (a) a bug, (b) allowed or even (c) mandatory according to the language standard?
- In case of (b) or (c), how? Which specific sentences in the standard allow/mandate the compiler to not find the
operator-at global scope? - How would you suggest to work around the issue, presuming that the
using namespacedirective is there to stay?