overloading

C++ - How to introduce overload set from variadic number of bases.

北城余情 提交于 2019-12-18 18:55:25
问题 The derived class hides the name of an overload set from the base class if the derived class has the same name defined, but we can always introduce that overload set back with using-declaration: template <class BASE> class A : public BASE { public: using BASE::some_method; void some_method(); } But what if I introduce all overload sets from variadic base classes? Would I be able to write something like this? template <class... BASES> class A : public BASES... { public: using BASES::some

Generic method to perform a map-reduce operation. (Java-8)

给你一囗甜甜゛ 提交于 2019-12-18 15:55:10
问题 How to overload a Function with generic parameter in Java 8? public class Test<T> { List<T> list = new ArrayList<>(); public int sum(Function<T, Integer> function) { return list.stream().map(function).reduce(Integer::sum).get(); } public double sum(Function<T, Double> function) { return list.stream().map(function).reduce(Double::sum).get(); } } Error: java: name clash: sum(java.util.function.Function<T,java.lang.Double>) and sum(java.util.function.Function<T,java.lang.Integer>) have the same

Overloading structs with template call operator and generic lambdas - gcc vs clang

若如初见. 提交于 2019-12-18 15:08:19
问题 I have discovered a code snippet that compiles and works properly in clang++ 4 (and trunk) but fails to compile in g++ 7 (and trunk) . Let's assume I have the following struct types: struct a { void foo() { } }; struct b { void bar() { } }; struct c { void bar() { } }; I want to create an overload set out of lambdas which handles a explicitly, while b and c are "caught" with a generic lambda using an auto parameter: auto ol = overload([](a x) { x.foo(); }, [](auto x){ x.bar(); }) When I

Choose function to apply based on the validity of an expression

房东的猫 提交于 2019-12-18 14:16:20
问题 The problem is the following, in C++14 : Let's have two functions FV&& valid_f , FI&& invalid_f , and arguments Args&&... args The function apply_on_validity should apply valid_f on args if the expression std::forward<FV>(valid_f)(std::forward<Args>(args)...) is valid Otherwise and if std::forward<FV>(invalid_f)(std::forward<Args>(args)...) is a valid expression, apply_on_validity should apply invalid_f on args Otherwise apply_on_validity should do nothing I guess the code should look like

How can I overload ASP.NET MVC Actions based on the accepted HTTP verbs?

吃可爱长大的小学妹 提交于 2019-12-18 14:15:23
问题 Wanted to use the same URL for a GET/PUT/DELETE/POST for a REST based API, but when the only thing different about the Actions is which HTTP verbs it accepts, it considers them to be duplicate! "Type already defines a member called 'Index' with the same parameter types." To which I said, so what? This one only accepts GET, this one only accepts POST... should be able to be co-exist right? How? 回答1: That's not ASP.NET MVC limitation or whatever. It's .NET and how classes work: no matter how

Formatting using DecimalFormat throws exception - “Cannot format given Object as a Number”

我与影子孤独终老i 提交于 2019-12-18 13:16:42
问题 This might look like a repeated question but I tried in all the below links and can't get a proper answer. Cannot format given Object as a Number ComboBox Illegal Argument Exception But I'm not getting what's wrong. Here is my code DecimalFormat twoDForm = new DecimalFormat("#.##"); double externalmark = 1.86; double internalmark = 4.0; System.out.println(String.valueOf((externalmark*3+internalmark*1)/4)); String val = String.valueOf((externalmark*3+internalmark*1)/4); String wgpa1=twoDForm

Overloading operator ->

本小妞迷上赌 提交于 2019-12-18 12:52:59
问题 Here is my code example: class X { public: void f() {} }; class Y : public X { public: X& operator->() { return *this; } void f() {} }; int main() { Y t; t.operator->().f(); // OK t->f(); // error C2819: type 'X' does not have an overloaded member 'operator ->' // error C2232: '->Y::f' : left operand has 'class' type, use '.' } Why the compiler is trying to "move the responsibility" for operator-> from Y to X? When I implement X::op-> then I cannot return X there - compile error says

Delphi/pascal: overloading a constructor with a different prototype

半腔热情 提交于 2019-12-18 11:47:30
问题 I'm trying to create a child class of TForm with a special constructor for certain cases, and a default constructor that will maintain compatibility with current code. This is the code I have now: interface TfrmEndoscopistSearch = class(TForm) public /// original constructor kept for compatibility constructor Create(AOwner : TComponent); overload; override; /// additional constructor allows for a caller-defined base data set constructor Create(AOwner : TComponent; ADataSet : TDataSet;

Why is a public const method not called when the non-const one is private?

前提是你 提交于 2019-12-18 10:58:13
问题 Consider this code: struct A { void foo() const { std::cout << "const" << std::endl; } private: void foo() { std::cout << "non - const" << std::endl; } }; int main() { A a; a.foo(); } The compiler error is: error: 'void A::foo()' is private`. But when I delete the private one it just works. Why is the public const method not called when the non-const one is private? In other words, why does overload resolution come before access control? This is strange. Do you think it is consistent? My code

Overloading null ambiguity

被刻印的时光 ゝ 提交于 2019-12-18 07:45:08
问题 I have the following methods: void Method(string param1, string param2); void Method(string param1, object param2); When I call the method using the following: method("string", null); It gives me an error because the call is ambiguous, the compiler does not know which version to call, because both methods accept null as the second parameter. How do I overcome this without changing the method name in one of them? the first method will never have null . 回答1: The problem is that both string and