oz1cz oz1cz - 1 year ago 40
C++ Question

Name lookup differences between g++ and MSVS

Consider this code:

#include <iostream>

namespace N {
class A {};
void f(A a) { std::cout << "N::f\n"; }

void f(int i) { std::cout << "::f\n"; }

template <typename T>
class Base {
void f(T x) { std::cout << "Base::f\n"; }

template <typename T>
class X : public Base<T> {
void g() {
T t;

int main()
X<N::A> x1;

X<int> x2;

The code is intended to investigate how name lookup works in C++.

If I compile this program with GNU C++ (version 6.1.0), it prints:


But if I compile it with Microsoft Visual Studio 2015, it prints:


Which is the correct behaviour, and why?

Answer Source

g++ is Standard compliant here, and Visual C++ not:

14.6.2 Dependent names [temp.dep]

3 In the definition of a class or class template, the scope of a dependent base class ( is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member.

Replacing f() with this->f() will find the base member.