overload-resolution

Overload resolution: assignment of empty braces

左心房为你撑大大i 提交于 2019-12-17 13:55:52
问题 I wrote some code S s; ... s = {}; , expecting it to end up the same as S s = {}; . However it didn't. The following example reproduces the problem: #include <iostream> struct S { S(): a(5) { } S(int t): a(t) {} S &operator=(int t) { a = t; return *this; } S &operator=(S const &t) = default; int a; }; int main() { S s = {}; S t; t = {}; std::cout << s.a << '\n'; std::cout << t.a << '\n'; } The output is: 5 0 My questions are: Why is operator=(int) selected here, instead of "ambiguous" or the

Overload resolution: assignment of empty braces

▼魔方 西西 提交于 2019-12-17 13:54:04
问题 I wrote some code S s; ... s = {}; , expecting it to end up the same as S s = {}; . However it didn't. The following example reproduces the problem: #include <iostream> struct S { S(): a(5) { } S(int t): a(t) {} S &operator=(int t) { a = t; return *this; } S &operator=(S const &t) = default; int a; }; int main() { S s = {}; S t; t = {}; std::cout << s.a << '\n'; std::cout << t.a << '\n'; } The output is: 5 0 My questions are: Why is operator=(int) selected here, instead of "ambiguous" or the

C# Method overload resolution not selecting concrete generic override

心不动则不痛 提交于 2019-12-17 10:56:28
问题 This complete C# program illustrates the issue: public abstract class Executor<T> { public abstract void Execute(T item); } class StringExecutor : Executor<string> { public void Execute(object item) { // why does this method call back into itself instead of binding // to the more specific "string" overload. this.Execute((string)item); } public override void Execute(string item) { } } class Program { static void Main(string[] args) { object item = "value"; new StringExecutor() // stack

What are the pitfalls of ADL?

非 Y 不嫁゛ 提交于 2019-12-17 03:02:33
问题 Some time ago I read an article that explained several pitfalls of argument dependent lookup, but I cannot find it anymore. It was about gaining access to things that you should not have access to or something like that. So I thought I'd ask here: what are the pitfalls of ADL? 回答1: There is a huge problem with argument-dependent lookup. Consider, for example, the following utility: #include <iostream> namespace utility { template <typename T> void print(T x) { std::cout << x << std::endl; }

C++ Insertion operator overload (<<)

我怕爱的太早我们不能终老 提交于 2019-12-13 15:19:30
问题 I'm working through an assignment in which I must overload the insertion operator to take a Node object. I've created the operator overload function outside the class definition, but inside the node.h file. Everything compiles fine, but the overloaded operator is not called, instead I get simple the address of the object. I'm prohibited from modifying the calling code, so any changes must be to the operator overload. My code as it stands right now: /** OPERATOR << ****************************

Why are const qualifiers in function arguments used for overloading resolution? [duplicate]

断了今生、忘了曾经 提交于 2019-12-13 11:59:56
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Functions with const arguments and Overloading I am pretty confused by the overloading and const declaration rules. Here are two things that puzzle me maybe you can help me find the deeper misunderstanding in my head that result in them being puzzling to me. ;) First issue: My compiler allows this: void f(int & x) { std::cout << "plain f" << std::endl; } void f(const int & x) { std::cout << "const f" << std:

Partial template specialization by overloading

青春壹個敷衍的年華 提交于 2019-12-13 06:26:20
问题 I created a simple round template function with an extra template argument that defines the type the rounded value needs to be casted to before returning. template <typename T, typename U> T round(U val) { T result; if (val >= 0) result = (T)(floor(val + (U)(.5))); else result = (T)(ceil( val - (U)(.5))); return result; } int a = round<int>(5.5); // = 6 // no compiler warnings But I also want the possibility to leave the extra template argument so that you don't have to insert the type you

In C++, why overload a function on a `const char array` and a private struct wrapping a `const char*`?

匆匆过客 提交于 2019-12-12 20:07:38
问题 I recently ran into a fascinating class in the ENTT library. This class is used to calculate hashes for strings like so: std::uint32_t hashVal = hashed_string::to_value("ABC"); hashed_string hs{"ABC"}; std::uint32_t hashVal2 = hs.value(); While looking at the implementation of this class I noticed that the none of the constructors or hashed_string::to_value member functions take a const char* directly. Instead, they take a simple struct called const_wrapper . Below is a simplified view of the

Overload Resolution: How is this not ambiguous?

喜你入骨 提交于 2019-12-12 15:12:33
问题 Suppose we have this code, copied from a separate question: namespace x { void f() { } class C { void f() { using x::f; f(); // <== } }; } The name f on the indicated line unambiguously refers to x::f (at least according to both gcc and clang). Why is x::f preferred over x::C::f in this case? Shouldn't it be ambiguous as both names are visible? 回答1: Because the using declaration brings x::f into the scope of f , which is narrower than that of C . Unqualified lookup considers the local block

Function Template Overload Resolution & Compiler Optimizations

一世执手 提交于 2019-12-12 12:07:21
问题 I was looking at this question found here Template function overload for type containing a type Where the OP user2079802 provided this code for his/her question: I'm trying to do the following: #include <iostream> #include <vector> #include <tuple> template <typename T> void f(T t) { std::cout << "1" << std::endl; } template <typename T, typename V> void f(T<std::tuple<V>> t) { std::cout << "2" << std::endl; } int main() { f(std::list<double>{}); // should use first template f(std::vector<std