overloading

Ambiguous call to overloaded function - std::to_string [duplicate]

不羁岁月 提交于 2019-12-01 02:49:14
This question already has an answer here: std::to_string - more than instance of overloaded function matches the argument list 2 answers In attempting to insert integer values into a string, I thought that my prayers were answered when I found std::to_string, but for some reason whenever I actually try to use it, Visual Studio complains about ambiguity. Here is the current incarnation of my function: string get_time_remaining (int elapsed) { string remaining; string temp_string; int time_remaining = TimeLimit - elapsed; int temp_int; temp_int = int(time_remaining / 3600); if(temp_int == 0)

Why does C# allow ambiguous function calls through optional arguments?

做~自己de王妃 提交于 2019-12-01 02:20:52
I came across this today, and I am surprised that I haven't noticed it before. Given a simple C# program similar to the following: public class Program { public static void Main(string[] args) { Method(); // Called the method with no arguments. Method("a string"); // Called the method with a string. Console.ReadLine(); } public static void Method() { Console.WriteLine("Called the method with no arguments."); } public static void Method(string aString = "a string") { Console.WriteLine("Called the method with a string."); } } You get the output shown in the comments for each method call. I

Why can't an interface implementation return a more specific type?

為{幸葍}努か 提交于 2019-12-01 02:05:49
If an interface specifies a property or method to return another interface, why is it not allowed for implementations of the first interface to "change" the return type into a more specific type? Let's take an example to illustrate: interface IFoo { IBar GetBar(); } interface IBar { } class Foo : IFoo { // This is illegal, we are not implementing IFoo properly public Bar GetBar() { return new Bar(); } } class Bar : IBar { } I know how to make it work, that's not my concern. I can just either: Change return type of GetFoo() to IBar , or Explicitly implement the interface and just call GetBar

Variable argument function ambiguity

不问归期 提交于 2019-12-01 01:56:23
问题 public static void main(String[] args) { System.out.println(fun(2,3,4)); } static int fun(int a,int b,int c) { return 1; } static int fun(int ... a) { return 0; } Output: 1 Question: In the above case why does the function fun select the 1st function and not the second.On what basis is the selection done since there is no way to determine which fun the user actually wanted to call ? 回答1: Basically there's a preference for a specific call. Aside from anything else, this means it's possible to

Performance differences between overloading or optional parameters?

妖精的绣舞 提交于 2019-12-01 01:19:45
No, not answered there ↑ I wonder if I should be using optional parameters in C#. Until now I was always overloading methods. But optional parameters are nice too, cleaner and less code. And I use them in other languages so I'm also used to them in some way. Is there anything that speaks against using them ? Performance is the first key point for me. Would it drop ? Example code: class Program { // overloading private static void test1(string text1) { Console.WriteLine(text1 + " " + "world"); } private static void test1(string text1, string text2) { Console.WriteLine(text1 + " " + text2); } //

Why is the compiler choosing this template function over an overloaded non-template function?

半城伤御伤魂 提交于 2019-12-01 00:32:13
问题 Using VC++ 2010, given the following: class Base { }; class Derived : public Base { }; template<class T> void foo(T& t); // A void foo(Base& base); // B Derived d; foo(d); // calls A foo(static_cast<Base&>(d)); // calls B I would like "B" to be called above. I can achieve this with a cast to Base , but why is this necessary? I want the template function to be called for all types not derived from Base (built-in types, etc.), but I want the non-template overload to be called for types derived

Different overloads with std::function parameters is ambiguous with bind (sometimes)

て烟熏妆下的殇ゞ 提交于 2019-12-01 00:12:33
I have two overloads of a function foo which take different std::function s which results in an ambiguity issue for the latter when used with the result of a std::bind . I don't understand why only this is ambiguous. void foo(std::function<void(int)>) {} void foo(std::function<int()>) {} void take_int(int) { } int ret_int() { return 0; } When using int() with a bind function I get an ambiguity error foo(std::bind(ret_int)); // ERROR With the gcc-5.1 error (and similar with clang) error: call to 'foo' is ambiguous foo(std::bind(ret_int)); ^~~ note: candidate function void foo(std::function<void

C++ std::stringstream operator<< overloading

走远了吗. 提交于 2019-11-30 23:49:00
问题 I have the following class(prototipe): class Token { public: //members, etc. friend std::stringstream& operator<< (std::stringstream &out, Token &t); }; And the operator is implemented like this: std::stringstream & operator<< (std::stringstream &out, Token &t) { out << t.getValue(); //class public method return out; } Now, I'm trying to use it like this: std::stringstream out; Token t; //initialization, etc. out << t; And VS gives me error, saying that there is no match for << operator. What

C++11: Overload fails to resolve recursive decltype

偶尔善良 提交于 2019-11-30 23:45:56
问题 In the following piece of code, I'm trying to build a lattice of types. For instance, between float and int , promote the result to float : float join(float f, int) { return f; } float join(float f, float) { return f; } Then I introduce a wrapper type: template <typename Inner> struct wrapper { using inner_t = Inner; inner_t value; }; whose behavior with the join operation quite natural: template <typename Inner1, typename Inner2> auto join(const wrapper<Inner1>& w1, const wrapper<Inner2>& w2

How to overload the 'new' method?

五迷三道 提交于 2019-11-30 23:30:16
I'm just started to learn Rust and I'm wondering if there is way to overload methods. At first I created a struct and used a 'impl' to implement basic 'new' method. Then I thought to add 'new' method with some params, and I tried to use trait for that. The following code was successfully compiled but once I tried to use 'new' with params, compiler gave me an error about extra params. So how should I overload methods in Rust? pub struct Words<'a> { pub nouns: Vec<&'a str>, } trait Test<'a>{ fn new(nouns: Vec<&'a str>) -> Self; } impl<'a> Words<'a> { pub fn new() -> Words<'a>{ let nouns = vec![