overloading

C# generic overload - Compiler can't determine correct call

时光总嘲笑我的痴心妄想 提交于 2020-01-02 04:15:09
问题 I don't understand why the compiler can't resolve the correct overload to use here. (code below) There is only one version of Add() that is appropriate- BigFoo is an IFoo, and does not implement IEnumerable where T is an IFoo. But it insists on reporting an ambiguity. Any ideas? I tried adding a second generic type parameter- Add where T : IFoo where U : IEnumerable. But then the overload is completely ignored even for legitimate use. I know I can work around this with casting and specifying

C++11: Universal executor

淺唱寂寞╮ 提交于 2020-01-02 04:11:08
问题 I would to know how get this code compiles: // test3.cpp #include <iostream> using namespace std; template<typename R, typename... rArgs> R universal_exer(R(*f)(rArgs...), rArgs... args) { return (*f)(forward<rArgs>(args)...); } int addition(int a) { return a; } int addition(int a, int b) { return a + b; } template<typename... Args> int addition(int a, int b, Args... args) { return a + b + addition(args...); } int main() { cout << universal_exer(&addition, 1) << endl; } Error message (gcc 4.7

Which is the more specialized template function? clang and g++ differ on that

回眸只為那壹抹淺笑 提交于 2020-01-02 00:13:11
问题 While playing with variadic templates, following this SO question (note: it is not mandatory to go there for following this question), I came to a different behavior of clang (3.8) and g++ (6.1) for the following template overloaded functions: template <class... Ts> struct pack { }; template <class a, class b> constexpr bool starts_with(a, b) { return false; } template <template <typename...> class PACK_A, template <typename...> class PACK_B, typename... Ts1, typename... Ts2> constexpr bool

C++ member function overloading with & (ampersand)

。_饼干妹妹 提交于 2020-01-01 23:10:54
问题 How to pick the correct error() member function? Do I need to cast somehow? using namespace std; struct test { int error(); void error(int x); int fun(); }; int main() { auto f1 = &test::error; // how to pick the correct function? auto f2 = &test::fun; // works } 回答1: Do I need to cast somehow? Yes, you can use static_cast. static_cast may also be used to disambiguate function overloads by performing a function-to-pointer conversion to specific type, as in std::transform(s.begin(), s.end(), s

Error with C++ operator overloading

☆樱花仙子☆ 提交于 2020-01-01 20:09:47
问题 #include<iostream> using namespace std; class complex { double real; double image; public: complex(double r=0,double i=0) : real(r), image(i) { }; complex(const complex& c) : real(c.real), image(c.image) { }; ~complex(){}; double re() const { return real; }; double im() const{ return image; }; const complex& operator =(const complex&c) { real = c.real; image = c.image; return *this; }; const complex& operator +=(const complex&c) { real += c.real; image += c.image; return *this; }; const

Function overloading vs Optional Parameters

倾然丶 夕夏残阳落幕 提交于 2020-01-01 12:11:13
问题 So I'm just thinking about function overloading... Overloaded methods share the same name but have a unique signature. The number of parameters, types of parameters or both must be different. A function can't be overloaded on the basis of a different return type alone. So in the following example, why overload setName rather than use optional parameters for the middle and last name values? class funOverload { public string name; //overloaded functions public void setName(string last) { name =

Why can't we overload a abstract method in a functional interface? (Java)

早过忘川 提交于 2020-01-01 12:08:14
问题 So I am familiar with functional interfaces in java, and their use with lambda expressions. A functional interface can only contain one abstract method. When using this lonely method from a lambda expression, you do not need to specify its name - since there is only one abstract method in the interface, the compiler knows that's the method you are referencing. Example: // Functional Interface: @FunctionalInterface public interface Ball { void hit(); } // Lambda to define, then run the hit

Why can't we overload a abstract method in a functional interface? (Java)

亡梦爱人 提交于 2020-01-01 12:07:13
问题 So I am familiar with functional interfaces in java, and their use with lambda expressions. A functional interface can only contain one abstract method. When using this lonely method from a lambda expression, you do not need to specify its name - since there is only one abstract method in the interface, the compiler knows that's the method you are referencing. Example: // Functional Interface: @FunctionalInterface public interface Ball { void hit(); } // Lambda to define, then run the hit

C++ calling completely wrong (virtual) method of an object

╄→尐↘猪︶ㄣ 提交于 2020-01-01 07:52:50
问题 I have some C++ code (written by someone else) which appears to be calling the wrong function. Here's the situation: UTF8InputStreamFromBuffer* cstream = foo(); wstring fn = L"foo"; DocumentReader* reader; if (a_condition_true_for_some_files_false_for_others) { reader = (DocumentReader*) _new GoodDocumentReader(); } else { reader = (DocumentReader*) _new BadDocumentReader(); } // the crash happens inside the following call // when a BadDocumentReader is used doc = reader->readDocument(

C++ calling completely wrong (virtual) method of an object

只谈情不闲聊 提交于 2020-01-01 07:52:05
问题 I have some C++ code (written by someone else) which appears to be calling the wrong function. Here's the situation: UTF8InputStreamFromBuffer* cstream = foo(); wstring fn = L"foo"; DocumentReader* reader; if (a_condition_true_for_some_files_false_for_others) { reader = (DocumentReader*) _new GoodDocumentReader(); } else { reader = (DocumentReader*) _new BadDocumentReader(); } // the crash happens inside the following call // when a BadDocumentReader is used doc = reader->readDocument(