Home > OS >  MSVC - Using namespace directive in caller of generic lambda leaks into the lambda's body
MSVC - Using namespace directive in caller of generic lambda leaks into the lambda's body

Time:09-21

Consider the following toy code:

#include <boost/hana/transform.hpp>
#include <range/v3/view/transform.hpp>

auto constexpr f = [](auto) {
    using namespace ranges::views;
    auto xxx = transform;
};

void caller() {
    using boost::hana::transform;
    f(1);
}

It compiles fine with GCC and MS' compiler, which means that using boost::hana::transform; is not affecting the names available in f's body, so it's unambiguous that xxx is ranges::views::transform.

On the other hand, if I change using boost::hana::transform; to using namespace boost::hana;, then Visual Studio claims that transform in f's body is an ambiguous name.

Is this a bug in GCC or Visual Studio? Is it a known bug? What is it due to?

(Demo.)

CodePudding user response:

It is an MSVC bug that has to do with generic lambdas. A minimal example is

void foo() {}

namespace B {
   void foo() {}
}

auto moo = [](auto) {
   foo();
};

int main() {
   using namespace B;
   moo(1);
}

It reproduces with c 17 and c 20 settings.

MSVC is known to have non-conforming handling of template instantiation and two-phase name lookup. Many of these bugs are fixed in the latest versions of the compiler, but apparently not all. Here, after moo is instantiated, the name foo is apparently looked up in the instantiation context. This should not happen because it is not a dependent name.

  • Related