name-lookup

namelookup with Unqualified name : C++0x draft n3290

霸气de小男生 提交于 2019-11-30 22:06:06
A point from the ISO C++ Draft n3290 : 3.4.0 2nd point A name “looked up in the context of an expression” is looked up as an unqualified name in the scope where the expression is found. Would someone please explain this statement with an example? It says that the scope which contains the expression will be searched for the name. i.e. namespace foo { struct bar { void foobar() { do_something(); } }; } if you have this code the name do_something will be searched in the scope of foobar , bar , foo and in the global scope (and not in other namespaces, structs or function scopes) 来源: https:/

static function lookup from a template function issue with xlC

╄→гoц情女王★ 提交于 2019-11-30 19:28:32
While I was searching for clues about a compilation problem I have had in my source, I have come across this bug report (against Mozilla's JavaScript engine source) related to functions lookup. Quoting from the bug report: TypedArrayTemplate is (obviously) a template, and it is referencing INT_TO_JSVAL, a static inline function, without prefixing it with "::". This breaks xlC because it can not resolve INT_TO_JSVAL. The standard does not require that statics be considered if the unqualified name is not found in the context of the template arguments. g++ does this fallback, xlC does not.

Private inheritance: name lookup error

怎甘沉沦 提交于 2019-11-30 13:56:19
问题 I have the following code example that doesn't compile: #include <stdio.h> namespace my { class base1 { // line 6 }; class base2: private base1 { }; class derived: private base2 { public: // The following function just wants to print a pointer, nothing else! void print(base1* pointer) {printf("%p\n", pointer);} }; } The error that gcc prints is: test.cpp:6: error: `class my::base1' is inaccessible test.cpp:17: error: within this context Now, i can guess what the problem is: when looking at

Ambiguous injected class name is not an error

只愿长相守 提交于 2019-11-30 11:08:14
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 considered to be a member of that class for the purposes of name hiding and lookup. From 9 (paragraph 2) A class-name is inserted into the scope in which it is declared immediately after the class-name is seen. The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. For purposes of access checking,

Difference between lookup rules for friend function defined inside vs outside of the class

佐手、 提交于 2019-11-30 09:37:36
问题 The following code: struct X { X() {} }; struct Y { Y() {} Y(X) {} Y(int) {} friend bool operator==(const Y&, const Y&) { return false; } }; bool f() { return 1 == X(); } fails to compile with the following error: error: no match for 'operator==' (operand types are 'int' and 'X') return 1 == X(); While if I move definition of operator== outside of the class it works just fine: struct X { X() {} }; struct Y { Y() {} Y(X) {} Y(int) {} friend bool operator==(const Y&, const Y&); }; inline bool

distance calculation error in c++ [closed]

谁说我不能喝 提交于 2019-11-30 07:43:52
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . #include <iostream> #include <cmath> #include <vector> using namespace std; int square(int a){ return a*a; } struct Point{ int x,y; }; int distance (const Point& a,const Point& b){ int k=(int) sqrt((float)((a.x-b

What is the fully qualified name of a friend function defined inside of a class?

巧了我就是萌 提交于 2019-11-30 06:33:40
问题 What is the fully qualified name of a friend function defined inside of a class? I recently saw an example analogous to the following. What is the fully qualified name of val() below? #include <iostream> namespace foo { class A { int x; public: A(int x = 0) : x(x) { } friend int val(const A &a) { return a.x; } }; } int main() { foo::A a(42); // val() found using ADL: std::cout << val(a) << std::endl; // foo::val(a); // error: 'val' is not a member of 'foo' // foo::A::val(a); // error: 'val'

Is this a bug in GCC?

浪尽此生 提交于 2019-11-30 05:39:36
问题 EDIT: This is not a bug, just me not knowing about dependent name lookups in templated base classes (which MSVC "helpfully" resolves without errors). I wrote a functor implementation a while back, and a simple "Event" wrapper that uses it. It compiles fine under MSVC, but GCC gives an error about a member variable in the base class, subscribers , not being declared; changing subscribers to this->subscribers resolves the issue(!). It appears to happen only with the curiously recurring template

Does overriding a non-const virtual method hide a const overload?

不打扰是莪最后的温柔 提交于 2019-11-30 04:40:22
问题 Consider: #include <iostream> using namespace std; struct A { virtual void f() { cout << "A::f" << endl; } virtual void f() const { cout << "A::f const" << endl; } }; struct B : public A {}; struct C : public A { virtual void f() { cout << "C::f" << endl; } }; int main() { const B b; b.f(); // prints "A::f const" const C c; c.f(); // Compile-time error: passing ‘const C’ as ‘this’ argument of // ‘virtual void C::f()’ discards qualifiers } (I'm using GCC.) So it seems that the const version of

static function lookup from a template function issue with xlC

依然范特西╮ 提交于 2019-11-30 03:36:58
问题 While I was searching for clues about a compilation problem I have had in my source, I have come across this bug report (against Mozilla's JavaScript engine source) related to functions lookup. Quoting from the bug report: TypedArrayTemplate is (obviously) a template, and it is referencing INT_TO_JSVAL, a static inline function, without prefixing it with "::". This breaks xlC because it can not resolve INT_TO_JSVAL. The standard does not require that statics be considered if the unqualified