name-lookup

C++ can't find function out of namespace

假装没事ソ 提交于 2021-02-19 02:56:08
问题 Compiling the following code fails because the second function can't find the first one, even though it's outside namespaces. I couldn't figure out the problem myself, and so far I haven't found any answers on the net. test.cpp: #include <bits/stdc++.h> struct myclass {}; template <typename T, typename U> std::ostream& operator<< (std::ostream &os, const std::pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } namespace my { void operator<< (std::ostream os, myclass

friend declaration of template specialization fails

爷,独闯天下 提交于 2021-02-18 11:16:48
问题 The following code containing friend declaration fails with indicated error (see http://ideone.com/Kq5dy): template<class T> void foo() {} template<typename T> class A { void foo(); friend void foo<T>(); // error: variable or field 'foo' declared void }; int main() { foo<int>(); } If the order of friend declaration and member function declaration reversed, then the code compiles without problems (see http://ideone.com/y3hiK): template<class T> void foo() {} template<typename T> class A {

Ambiguous multiple inheritance of template classes

若如初见. 提交于 2021-02-08 12:32:14
问题 I've got a real situation which can be summarized in the following example: template< typename ListenerType > struct Notifier { void add_listener( ListenerType& ){} }; struct TimeListener{ }; struct SpaceListener{ }; struct A : public Notifier< TimeListener > , public Notifier< SpaceListener > { }; struct B : TimeListener{ }; int main() { A a; B b; a.add_listener( b ); // why is ambiguous? return 0; } Why is not obvious to the compiler that B is a TimeListener , and therefore the only

Name resolution of functions inside templates instantiated with qualified types

三世轮回 提交于 2021-02-07 12:52:24
问题 Consider the following C++ code example: namespace n { struct A {}; } struct B {}; void foo(int) {} template<typename T> void quux() { foo(T()); } void foo(n::A) {} void foo(B) {} int main() { quux<n::A>(); // Error (but works if you comment out the foo(int) declaration) quux<B>(); // Works return 0; } As indicated in the comment, the template instantiation quux<n::A>() causes a compiler error (on GCC 4.6.3): foo.cpp: In function ‘void quux() [with T = n::A]’: foo.cpp:22:16: instantiated from

Should this function call be ambiguous?

独自空忆成欢 提交于 2021-01-27 04:56:30
问题 I stumbled on this the other day and can't figure out which answer is correct, or whether both are acceptable. Specifically, I'm referring to the call to bar(T{}) in OtherFunction. From what I've been able to test on compiler explorer, the decision seems split. msvc and icc agree that it is ambiguous while gcc and clang compile the code without issue. The function bar inside the namespace hidden becomes visible through argument-dependent lookup. Additionally, msvc/icc consider the declaration

Is this-> mandatory to access Base<T> identifiers from derived classes?

蹲街弑〆低调 提交于 2020-08-18 08:29:19
问题 This code compiles with MSVC 2015, but doesn't compile with Clang 5.0.0 (trunk 304874): template <typename T> struct Base { T data; }; template <typename T> struct Derived : Base<T> { auto getData() const { return data; } }; Replacing data with this->data in Derived::getdata() makes Clang happy. Which compiler is correct according to the C++ standard? Must this-> be used in template code to access an identifier of a base class? 回答1: Clang is correct. $17.6.2/3 Dependent names [temp.dep] In

When is there an UB because the best overload match was not found by ADL at the point of instantiation?

℡╲_俬逩灬. 提交于 2020-08-02 07:47:32
问题 When a function body is instantiated, dependent function call overload resolution should find the best match in associated namespace through ADL, otherwise the behavior is undefined, [temp.dep.candidate]§1 If the call would be ill-formed or would find a better match had the lookup within the associated namespaces considered all the function declarations with external linkage introduced in those namespaces in all translation units, not just considering those declarations found in the template

Two-phase function template compilation: not *only* ADL is employed in the 2nd phase?

旧时模样 提交于 2020-06-14 06:33:47
问题 I'm wondering why the following code compiles. #include <iostream> template<class T> void print(T t) { std::cout << t; } namespace ns { struct A {}; } std::ostream& operator<<(std::ostream& out, ns::A) { return out << "hi!"; } int main() { print(ns::A{}); } I was under impression that at the instantiation point unqualified dependent names are looked-up via ADL only - which should not consider the global namespace. Am I wrong? 回答1: This is an interesting case. The workings of name lookup as

Overload Resolution/Ambiguity in name lookup(which one)

谁都会走 提交于 2020-02-15 07:56:08
问题 $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

Overload Resolution/Ambiguity in name lookup(which one)

倖福魔咒の 提交于 2020-02-15 07:55: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