name-lookup

cannot access namespace scope friend explicitly

狂风中的少年 提交于 2019-12-10 22:38:58
问题 I had an issue today where ADL wasn't finding a static member function for a type defined inside a class. That is, in the below example, str(foo::Foo::Enum) isn't located via ADL without explicitly scoping it, foo::Foo::str(foo::Foo::Enum) namespace foo { struct Foo { enum Enum { FOO1, FOO2 }; static const char* str(Enum e); }; } foo::Foo::Enum e = foo::Foo::FOO1; const char* s = str(e); // ADL doesn't work I found this SO question, and as stated in the accepted answer, changing it to a

Overloading member function among multiple base classes

扶醉桌前 提交于 2019-12-10 18:02:07
问题 Basically I want to have multiple member functions with same name, but different signature, spread in multiple base classes. Example: #include <iostream> struct A { void print(int) { std::cout << "Got an int!" << std::endl; } }; struct B { void print(double) { std::cout << "Got a double!" << std::endl; } }; struct C : A, B {}; int main() { C c; c.print((int)0); return 0; }; But I got this error on clang: main.cpp:18:7: error: member 'print' found in multiple base classes of different types c

Ambiguous member template lookup

时光毁灭记忆、已成空白 提交于 2019-12-10 16:36:07
问题 An answer to this question says in the following code: #include <vector> using std::vector; struct foo { template<typename U> void vector(); }; int main() { foo f; f.vector<int>(); // ambiguous! } The last line in main is ambiguous, because the compiler not only looks up vector within foo , but also as an unqualified name starting from within main . So it finds both std::vector and foo::vector . To fix this, you have to write f.foo::vector<int>(); I've tried this program on all popular C++

Derived template-class access to base-class member-data

时间秒杀一切 提交于 2019-12-10 16:33:20
问题 This question is a furtherance of the one asked in this thread. Using the following class definitions: template <class T> class Foo { public: Foo (const foo_arg_t foo_arg) : _foo_arg(foo_arg) { /* do something for foo */ } T Foo_T; // either a TypeA or a TypeB - TBD foo_arg_t _foo_arg; }; template <class T> class Bar : public Foo<T> { public: Bar (const foo_arg_t bar_arg, const a_arg_t a_arg) : Foo<T>(bar_arg) // base-class initializer { Foo<T>::Foo_T = T(a_arg); } Bar (const foo_arg_t bar

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

廉价感情. 提交于 2019-12-09 09:09:09
问题 The following very simple code won't compile #include <vector> #include <string> namespace Foobar { struct Test { std::string f; std::uint16_t uuid; }; } bool operator==(const Foobar::Test& lhs, const Foobar::Test& rhs){ return lhs.f == rhs.f && lhs.uuid == rhs.uuid; } int main(){ std::vector<Foobar::Test> a; std::vector<Foobar::Test> b; if(a==b){ } return 0; } https://godbolt.org/g/zn6UgJ Won't compile in any of the compilers I have. While the following #include <vector> #include <string>

Can't understand name lookup differences between an int and a user defined type - perhaps ADL related

ぐ巨炮叔叔 提交于 2019-12-08 16:12:12
问题 Why does the following code compile: template<typename T> void foo(T in) { bar(in); } struct type{}; void bar(type) {} int main() { foo(type()); } When the following does not: template<typename T> void foo(T in) { bar(in); } void bar(int) {} int main() { foo(42); } Compiling with GnuC++ 7: a.cpp: In instantiation of 'void foo(T) [with T = int]': a.cpp:9:20: required from here a.cpp:2:21: error: 'bar' was not declared in this scope, and no declarations were found by argument-dependent lookup

Why is a program rejected as ambiguous that could be resolved by overload resolution?

女生的网名这么多〃 提交于 2019-12-08 15:58:13
问题 The following program is rejected by gcc as ambiguous: struct Aint { virtual void foo(int); }; struct Astring { virtual void foo(std::string); }; struct A: public Aint, public Astring {}; int main() { std::string s; A a; a.foo(s); return 0; } > vt.cpp: In function ‘int main()’: vt.cpp:13:9: error: request for > member ‘foo’ is ambiguous > a.foo(s); > ^ vt.cpp:5:34: note: candidates are: virtual void Astring::foo(std::__cxx11::string) > struct Astring {virtual void foo(std::string);}; > ^ vt

When does overload resolution of non-dependent name take place, in definition context or point of instantiation?

隐身守侯 提交于 2019-12-07 09:04:31
问题 3.4 [basic.lookup]/p1 Overload resolution (13.3) takes place after name lookup has succeeded. void g(long); void g(int, int); template<class T> void f() { g(0); } void g(int, int = 0) {} int main(){ f<int>(); } gcc compiles succeed, clang faild. When does overload resolution of non-dependent name take place, in definition context or point of instantiation? Or both are right? 回答1: In both context. [temp.res] 14.6\8 If a hypothetical instantiation of a template immediately following its

Why does C++11 not support name lookup like this? [closed]

青春壹個敷衍的年華 提交于 2019-12-07 06:40:22
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . struct A { enum InnerEnum { X }; A(InnerEnum x) {} }; int main() { A a(X); } The compiler complains: error C2065: 'X' : undeclared

Name lookup for overloaded functions defined later

你离开我真会死。 提交于 2019-12-06 03:47:14
问题 I noticed strange behavior regarding function lookup when relying on a function to be defined later: #include <iostream> template <typename T> void foo(const T&) { std::cout << "Basic" << std::endl; } template <typename T> void bar() { T x; foo(x); } void foo(const int& x) { std::cout << "int:" << x << std::endl; } int main() { bar<int>(); } Output: Basic For some reason, I expected the use of foo inside bar to find the overload below it. Moving the overload of foo to above bar makes the