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
23
votes
6 answers

Ambiguous member access expression: is Clang rejecting valid code?

I have some code that, for the purposes of this question, boils down to template class TemplateClass : public T { public: void method() {} template static void static_method(U u) { u.TemplateClass::method();…
Per
  • 2,594
  • 12
  • 18
23
votes
2 answers

Why does this template function not behave as expected?

I was reading about template functions and got confused by this problem: #include void f(int) { std::cout << "f(int)\n"; } template void g(T val) { std::cout << typeid(val).name() << " "; f(val); } void…
23
votes
2 answers

Why is this call to swap() ambiguous?

The following program #include #include #include namespace my_namespace { template void swap(T& a, T& b) { T tmp = std::move(a); a = std::move(b); b = std::move(tmp); } template
20
votes
1 answer

hiding of template parameter of member template

from temp.local : In the definition of a member of a class template that appears outside of the class template definition, the name of a member of the class template hides the name of a template-parameter of any enclosing class templates (but…
Volodymyr Boiko
  • 1,533
  • 15
  • 29
20
votes
2 answers

Name lookup differences between g++ and MSVS

Consider this code: #include namespace N { class A {}; void f(A a) { std::cout << "N::f\n"; } } void f(int i) { std::cout << "::f\n"; } template class Base { public: void f(T x) { std::cout << "Base::f\n";…
oz1cz
  • 5,504
  • 6
  • 38
  • 58
19
votes
2 answers

Ambiguous injected class name is not an error

What I read in the C++ standard about injected class names contradicts (as I see it) with the behavior of a sample program I will present shortly. Here's what I read: From 3.4 (paragraph 3) The injected-class-name of a class (clause 9) is also…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
19
votes
2 answers

Cannot declare an operator within a function. Clang bug or spec?

One of the weirder corner cases of C is that functions can be declared within other functions, e.g. void foo(void) { void bar(void); // Behaves as if this was written above void foo(void) bar(); } This has carried through into C++, at least for…
Jon Chesterfield
  • 2,251
  • 1
  • 20
  • 30
16
votes
1 answer

What is the rule that allows `this->` to access members of dependent base classes?

As we know, the code below is ill-formed because the member x is in a dependent base class. However, changing x to this->x on the indicated line would fix the error. template struct B { int x; }; template struct C :…
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
15
votes
1 answer

Template parameter name hiding

I got recently bitten by (simplified) struct Base { typedef char T; }; template struct Foo : Base { T x[50]; // This is Base::T, not the template parameter }; In other words a class member name hides a template parameter (even…
6502
  • 112,025
  • 15
  • 165
  • 265
14
votes
3 answers

Is a fully qualified class name down to global scope ever required for out-of-line member function definitions?

This question got me wondering whether it is ever useful/necessary to fully qualify class names (including the global scope operator) in an out-of-class member function definition. On the one hand, I've never seen this done before (and the syntax to…
14
votes
1 answer

Comparison operator for std::vector fails to find comparison operator for T

The following very simple code won't compile #include #include namespace Foobar { struct Test { std::string f; std::uint16_t uuid; }; } bool operator==(const Foobar::Test& lhs, const Foobar::Test& rhs){ …
14
votes
1 answer

Scope resolution operator being used twice

namespace libzerocoin { //Commitment class Commitment::Commitment::Commitment(const IntegerGroupParams* p, const Bignum& value): params(p), contents(value) { this->randomness =…
Eloy
  • 386
  • 2
  • 13
14
votes
1 answer

Is a namespace required when referring to the base class

I have code like this: namespace N { class B { public: virtual void doStuff(B *) = 0; }; } // not in a namespace class Derived : public N::B { public: void doStuff(B *); // Should this be N::B, or is B ok? }; Do I…
user9876
  • 10,954
  • 6
  • 44
  • 66
13
votes
1 answer

Private inheritance: name lookup error

I have the following code example that doesn't compile: #include namespace my { class base1 { // line 6 }; class base2: private base1 { }; class derived: private base2 { public: // The…
anatolyg
  • 26,506
  • 9
  • 60
  • 134
13
votes
2 answers

friend class declaration and using directive

Is the following example well-formed? namespace N { class A; } using namespace N; class B { int i; friend class A; }; namespace N { class A { B m; int get() { return m.i; } }; } This example compiled…
Mitsuru Kariya
  • 938
  • 4
  • 14
1
2
3
20 21