name-lookup

Is a namespace required when referring to the base class

江枫思渺然 提交于 2019-12-03 16:48:18
问题 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 need the namespace qualifier where Derived refers to it's base class? GCC and MSVC are happy with the code as written, but another compiler complains unless I put the namespace in. What does the C++ standard say? 回答1: Inside the class definition B is OK. That's the so-called

Conditional operator's return type and two-phase lookup

这一生的挚爱 提交于 2019-12-03 14:24:59
问题 Consider the following snippet: struct Base { }; struct Derived : Base { }; void f(Base &) { std::cout << "f(Base&)\n"; } template <class T = int> void g() { Derived d; f(T{} ? d : d); // 1 } void f(Derived &) { std::cout << "f(Derived&)\n"; } int main() { g(); } In this case, I reckon that the function call to f at // 1 should be looked up in phase one, since its argument's type is unambigously Derived& , and thus be resolved to f(Base&) which is the only one in scope. Clang 3.8.0 agrees

Special behavior for decltype of call operator for incomplete types

走远了吗. 提交于 2019-12-03 13:31:28
I've been struggling with a compilation issue, and have been able to shrink the problem down to a small code segment. To set the stage, I'm trying to do CRTP, where the base method calls another in the derived class. The complication is, I want to use trailing return types to get the type of forwarding directly to the the Derived class's method. This always fails to compile unless I forward to the call operator in the derived class. This compiles: #include <utility> struct Incomplete; template <typename Blah> struct Base { template <typename... Args> auto entry(Args&&... args) -> decltype(std:

Erroneous private base class inaccessible?

无人久伴 提交于 2019-12-03 13:18:05
Compiling this code using g++ 4.2.1: struct S { }; template<typename T> struct ST { }; template<typename BaseType> class ref_count : private BaseType { }; template<typename RefCountType> class rep_base : public RefCountType { }; class wrap_rep : public rep_base<ref_count<S> > { typedef rep_base<ref_count<S> > base_type; // line 11 }; I get: bug.cpp:1: error: ‘struct S’ is inaccessible bug.cpp:11: error: within this context However, if I change the wrap_rep class to use ST : class wrap_rep : public rep_base<ref_count< ST<int> > > { typedef rep_base<ref_count< ST<int> > > base_type; }; it

Why is this call to swap() ambiguous?

被刻印的时光 ゝ 提交于 2019-12-03 10:39:37
The following program #include <algorithm> #include <utility> #include <memory> namespace my_namespace { template<class T> void swap(T& a, T& b) { T tmp = std::move(a); a = std::move(b); b = std::move(tmp); } template<class T, class Alloc = std::allocator<T>> class foo {}; } int main() { my_namespace::foo<int> *a, *b; using my_namespace::swap; swap(a,b); return 0; } causes both g++ and clang to issue the following compiler error on my system: $ clang -std=c++11 swap_repro.cpp -I. swap_repro.cpp:28:3: error: call to 'swap' is ambiguous swap(a,b); ^~~~ /usr/bin/../lib/gcc/x86_64-linux-gnu/5.2.1/

hiding of template parameter of member template

╄→尐↘猪︶ㄣ 提交于 2019-12-03 06:41:56
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 not a template-parameter of the member if the member is a class or function template ). [ Example: template<class T> struct A { struct B { /* ... */ }; typedef void C; void f(); template<class U> void g(U); }; template<class B> void A<B>::f() { B b; // A's B, not the template parameter } template<class B> template<class C> void A<B>::g(C) { B b; // A's B

Conditional operator's return type and two-phase lookup

廉价感情. 提交于 2019-12-03 05:15:40
Consider the following snippet: struct Base { }; struct Derived : Base { }; void f(Base &) { std::cout << "f(Base&)\n"; } template <class T = int> void g() { Derived d; f(T{} ? d : d); // 1 } void f(Derived &) { std::cout << "f(Derived&)\n"; } int main() { g(); } In this case, I reckon that the function call to f at // 1 should be looked up in phase one, since its argument's type is unambigously Derived& , and thus be resolved to f(Base&) which is the only one in scope. Clang 3.8.0 agrees with me , but GCC 6.1.0 doesn't , and defers the lookup of f until phase two, where f(Derived&) is picked

Overload Resolution/Ambiguity in name lookup(which one)

冷暖自知 提交于 2019-12-01 19:50:55
$7.3.3/14 (C++03) struct A { int x(); }; struct B : A { }; struct C : A { using A::x; int x(int); }; struct D : B, C { using C::x; int x(double); }; int f(D* d) { return d->x(); // ambiguous: B::x or C::x } The comment in the code in 'f' indicates that one can expect ambiguity between 'B::x' or 'C::x'. However, on compiling with g++(ideone) or Comeau the errors are slightly different. These errors instead of indicating ambiguity in B::x or C::x indicate the fact that A is an ambiguous base of D prog.cpp: In function ‘int f(D*)’: prog.cpp:16: error: ‘A’ is an ambiguous base of ‘D’ And

Name Lookup and class scope

删除回忆录丶 提交于 2019-12-01 10:53:23
Why is it that the return type of setVal is of type string and the parameter type is of type double typedef string Type; Type initVal(); class Exercise { public: typedef double Type; Type setVal(Type); Type initVal(); private: int val; }; Type Exercise::setVal(Type parm) { val = parm + initVal(); return val; } When member functions are defined in namespace scope C++ provides special name lookup rules for unqualified names that follow the function’s declarator-id (3.4.1/8). Such names are looked up in class scope before they are looked up in namespace scope. Since the return type in an

Name Lookup and class scope

非 Y 不嫁゛ 提交于 2019-12-01 08:07:58
问题 Why is it that the return type of setVal is of type string and the parameter type is of type double typedef string Type; Type initVal(); class Exercise { public: typedef double Type; Type setVal(Type); Type initVal(); private: int val; }; Type Exercise::setVal(Type parm) { val = parm + initVal(); return val; } 回答1: When member functions are defined in namespace scope C++ provides special name lookup rules for unqualified names that follow the function’s declarator-id (3.4.1/8). Such names are