Questions tagged [name-lookup]

Name lookup is the procedure by which a name, when encountered in a program, is associated with the declaration that introduced it.

For function names, name lookup can associate multiple declarations with the same name, and may obtain additional declarations from . Template argument deduction may also apply, and the set of declarations is passed to overload resolution, which selects the declaration that will be used. Member access rules, if applicable, are considered only after name lookup and overload resolution. For all other names (variables, namespaces, classes, etc), name lookup must produce a single declaration in order for the program to compile.

Unqualified name lookup

For an unqualified name, that is name that does not appear to the right of a scope resolution operator ::, name lookup examines the scopes as described below, until it finds at least one declaration of any kind, at which time the lookup stops and no further scopes are examined.

For the purpose of unqualified name lookup, all declarations from a namespace mentioned by a using directive are members of the namespace in which the using-directive appears.

303 questions
3
votes
3 answers

Class member function defined outside its namespace

The following code compiles perfectly with the latest MSVC, GCC and CLang available at godbolt online compiler explorer site. I wonder why: namespace ns { struct Test { void foo(); }; } using namespace ns; // Alert! Member…
Igor G
  • 1,838
  • 6
  • 18
3
votes
0 answers

Why is the scope operator for std namespace not required in this case?

I tried the following: #include #include int main () { std::vector myVector = {1, 2, 3, 4}; all_of(myVector.begin(), myVector.end(), [](int i){return i;}); } expecting a compilation error as I did not put std:: in…
User
  • 163
  • 6
3
votes
1 answer

Why class::class::class::staticClassMember() compiles (in C++)?

I must have missed something in C++ specification because I can't explain why the following code compiles successfully: class MyClass { static void fun(); }; int main() { MyClass::MyClass::MyClass::fun(); } Could somebody point me to the standard…
Jarek C
  • 1,077
  • 6
  • 18
3
votes
1 answer

C++ name resolution rules for otherwise identical names with different scope

I realize that the following is horrible style, but for the sake of argument, assume I have the following code: struct parent { virtual ~parent() {} }; struct child : public parent { child() {} virtual ~child() {} }; struct…
phonetagger
  • 7,701
  • 3
  • 31
  • 55
3
votes
1 answer

How do name-lookup and operator-overload work?

I want to output some private library class ns::A to the plog, so I add the operator << overload to ns::A. The following code cannot be compiled. error: no match for ‘operator<<’ (operand types are ‘std::ostringstream’ {aka…
user2709407
  • 460
  • 1
  • 4
  • 11
3
votes
1 answer

<< operator override compiles with g++ not windows

I am trying to port an application to win-dows (ironic, I know). The following bare-bone example illustrates the problem. I get the following error when compiling with VS12 and VS14: C2679 binary '<<': no operator found which takes a right-hand…
atomSmasher
  • 1,465
  • 2
  • 15
  • 37
3
votes
2 answers

Inconsistency in name lookup among different compilers

First of all feel free to suggest better title for this question. Consider following program: #include namespace N { class C {}; } int operator+( int i, N::C ) { return i+1; } int main() { N::C a[10]; std::accumulate( a, a+10, 0…
3
votes
1 answer

C++ override function from same base template class with multiple inheritance ambiguous function call

I need to call init(int* iNumber) function which is derived from the base class. BaseClass.h #pragma once #include "stdafx.h" template class BaseClass { public: BaseClass() {} virtual ~BaseClass() {} virtual void init(T*…
3
votes
1 answer

C++ multiple inheritance private member ambigious access

The following code: class A1 { public: int x; }; class A2 { private: int x() { return 67; } }; class M : public A1, public A2 {}; int main() { M m; m.x; } Compiles with error: error C2385: ambiguous access of 'x' note: could be…
3
votes
1 answer

Declare function after template defined

Let's say I have a template function: template void tfoo( T t ) { foo( t ); } later I want to use it with a type, so I declare/define a function and try to call it: void foo( int ); int main() { tfoo(1); } and I am getting error…
Slava
  • 43,454
  • 1
  • 47
  • 90
3
votes
1 answer

Name hiding by using declaration

#include struct H { void swap(H &rhs); }; void swap(H &, H &) { std::cout << "swap(H &t1, H &t2)" << std::endl; } void H::swap(H &rhs) { using std::swap; swap(*this, rhs); } int main(void) { H a; H b; …
3
votes
2 answers

Why c++ compiler (VS2013) chooses wrong function?

First case #include class A { public: virtual void Write(int i) { std::wcout << L"Write(int) is called" << std::endl; } virtual void Write(wchar_t c) { std::wcout << L"Write(wchar_t) is called" <<…
3
votes
2 answers

How does unqualified name lookup work when using using-declarations?

Is this ill-formed or well-formed according to the c++ standard? namespace M { struct i {}; } namespace N { static int i = 1; } using M::i; using N::i; int main() { sizeof (i); } Clang rejects it and GCC accepts it. According to [namespace.udir-6]…
3
votes
2 answers

What other names could be considered during the lookup for a namespace-name, different than namespace names?

§3.4.6/1: In a using-directive or namespace-alias-definition, during the lookup for a namespace-name or for a name in a nested-name-specifier only namespace names are considered. Basically, what I'm asking is: "why is this paragraph…
Mao
  • 1,065
  • 8
  • 12
3
votes
1 answer

Accessing base member functions in class derived from template class

I am developing a library at my work and I have designed a complicated inheritance that includes template classes and deriving from them. My problem is that a base template class has virtual overloaded operator that takes 2 arguments and returns…