name-lookup

Ambiguous injected class name is not an error

拜拜、爱过 提交于 2019-11-29 16:39:49
问题 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

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

匆匆过客 提交于 2019-11-29 16:16:05
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 operator==(const Y&, const Y&) { return false; } bool f() { return 1 == X(); } Could someone explain why

Unqualified name lookup: Why local declaration hides declaration from using directive

China☆狼群 提交于 2019-11-29 14:04:31
Consider this code: namespace A { int i = 24; } namespace B { using namespace A; int i = 11; int k = i; // finds B::i, no ambiguity } And basic.lookup.unqual.2 : §6.4.1 Unqualified name lookup [basic.lookup.unqual] The declarations from the namespace nominated by a using-directive become visible in a namespace enclosing the using-directive; see [namespace.udir]. For the purpose of the unqualified name lookup rules described in [basic.lookup.unqual], the declarations from the namespace nominated by the using-directive are considered members of that enclosing namespace. For me the standard says

Why class member functions shadow free functions with same name?

≡放荡痞女 提交于 2019-11-29 09:51:44
It recently came to my attention that member functions completely shadow free functions with the same name when inside the class. And by completely i mean that every free function with the same name is not considered for overload resolution at all. I can understand why it's done with somwthing like this: void f(); struct S { void f(); void g() { f(); // calls S::f instead of ::f } }; where the functions have identical signatures, its only natural as variable scoping works the same way. But why prohibit unambigious calls where free function has different signature like this: void f(); struct S

distance calculation error in c++ [closed]

不羁的心 提交于 2019-11-29 05:22:48
#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.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y))); return k; } int main(){ vector<Point>a(10); for (int i=0;i<10;i++){ cin>>a[i].x>>a[i].y; } int s=0; int s1; int k=0; for (int i=1;i<10;i++){ s+=square(distance(a[0],a[i])); } for (int i=1;i<10;i++){ s1=0; for (int j=0;j<10;j++){ s1+=square(distance(a[i],a[j])); if (s1<s) { s=s1; k=i;} } } cout<<k<<"Points are:"; cout<<a[k].x; cout<<a[k].y; return

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

女生的网名这么多〃 提交于 2019-11-29 01:29:53
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 <typename T> struct B { int x; }; template <typename T> struct C : B<T> { void f() { int y = x; // Error! } }; int main() { C<int> c; c.f(); } I would like an explanation of how this behaviour is specified in the standard. According to [temp.dep]/3: In the definition of a class or class template, if a base class depends on a template-parameter, the base class scope is not examined during unqualified name lookup either at

Is ADL the only way to call a friend inline function?

江枫思渺然 提交于 2019-11-28 22:56:22
Let us define f , as a friend function of S , inside the declaration of S : struct S { friend void f() {} }; I cannot find a way to call f . Is it true, then, that such an inline friend function can only be called with argument-dependant lookup ? struct S { friend void f() {} friend void g(S const&) {} } const s; int main() { // f(); // error: 'f' was not declared in this scope // S::f(); // error: 'f' is not a member of 'S' g(s); // S::g(s); // error: 'g' is not a member of 'S' } Bonus: what if I want to get a function-pointer/ std::function /lambda to g ? Is it true, then, that such an

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

六月ゝ 毕业季﹏ 提交于 2019-11-28 20:03:43
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 not a member of 'foo::A' return 0; } Is argument-dependent lookup the only way val() can be found?

Scope resolution operator being used twice

自闭症网瘾萝莉.ら 提交于 2019-11-28 00:56:47
namespace libzerocoin { //Commitment class Commitment::Commitment::Commitment(const IntegerGroupParams* p, const Bignum& value): params(p), contents(value) { this->randomness = Bignum::randBignum(params->groupOrder); this->commitmentValue = (params->g.pow_mod(this->contents, params->modulus).mul_mod( params->h.pow_mod(this->randomness, params->modulus), params->modulus)); } I just encountered this function definition on GitHub . I assume that the second and the third "Commitment" refer to the class name and constructor, but I can't figure out the meaning of the first. I am sure that it does

Two phase name lookup for C++ templates - Why?

荒凉一梦 提交于 2019-11-27 14:18:17
Why does the C++ standard define two phase lookup for templates? Couldn't non dependent declarations and definitions' lookups be deferred to the instantiation stage as well? James Kanze They could. This is the way most early implementations of templates worked, and is still the way the Microsoft compiler worked. It was felt (in the committee) that this was too error prone; it made it too easy to accidentally hijack a name, with the instantiation in one translation unit picking up a local name, rather than the desired global symbol. (A typical translation unit will consist of a sequence of