overloading

Encapsulating Action<T> and Func<T>?

ぐ巨炮叔叔 提交于 2019-12-04 10:30:45
问题 I'm trying to make a design for some sort of IExecutable interface. I will not get into details, but the point is that I have several Actions that need to be executed from a base class. They may take different parameters (no big deal), and they may/may not return a value. So far, this is my design: public abstract class ActionBase { // ... snip ... } public abstract class ActionWithResultBase<T>: ActionBase { public abstract T Execute(); } public abstract class ActionWithoutResultBase:

What exactly is a “trailing parameter pack”

我的梦境 提交于 2019-12-04 10:17:43
问题 In resolving ambiguities between function template overloads, partial ordering is performed (see here for some explanations). In that website, we also learn that In case of a tie, if one function template has a trailing parameter pack and the other does not, the one with the omitted parameter is considered to be more specialized than the one with the empty parameter pack. Now, I wonder what precisely a trailing parameter pack is. Which if any of template<class ...> struct tuple { /* ... */ };

Function overloading vs Optional Parameters

情到浓时终转凉″ 提交于 2019-12-04 10:14:56
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 = last; } public void setName(string first, string last) { name = first + "" + last; } public void

Why would anyone want to overload the & (address-of) operator? [duplicate]

别来无恙 提交于 2019-12-04 10:01:45
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What legitimate reasons exist to overload the unary operator& ? I just read this question, and I can't help but wonder: Why would anyone possibly want to overload the & ("address-of") operator? some_class* operator&() const { return address_of_object ; } Is there any legitimate use case? 回答1: If you're dealing with any sort of wrapper objects, you might want or need to transparently forward the access to the

Use invokedynamic to implement multiple dispatch

£可爱£侵袭症+ 提交于 2019-12-04 09:31:02
问题 I wondered if Java7's new invokedynamic bytecode instruction could be used to implement multiple dispatch for the Java language. Would the new API under java.lang.invoke be helpful to perform such a thing? The scenario I was thinking about looked as follows. (This looks like an application case for the visitor design pattern, but there may be reasons that this is not a viable option.) class A {} class A1 extends A {} class A2 extends A {} class SomeHandler { private void doHandle(A1 a1) { ...

Java method keyword “final” and its use

安稳与你 提交于 2019-12-04 08:18:32
When I create complex type hierarchies (several levels, several types per level), I like to use the final keyword on methods implementing some interface declaration. An example: interface Garble { int zork(); } interface Gnarf extends Garble { /** * This is the same as calling {@link #zblah(0)} */ int zblah(); int zblah(int defaultZblah); } And then abstract class AbstractGarble implements Garble { @Override public final int zork() { ... } } abstract class AbstractGnarf extends AbstractGarble implements Gnarf { // Here I absolutely want to fix the default behaviour of zblah // No Gnarf shouldn

Function overloading vs. default parameters in VB.NET?

六月ゝ 毕业季﹏ 提交于 2019-12-04 07:42:06
In VB.NET, which is better to use: function overloading or default parameters? Is the code going to be used by other languages? If so, that swings the balance towards overloads while still bearing Hamish's answer in mind. In particular, C# doesn't support optional parameters - yet... Admittedly this wouldn't actually prevent someone using your code from C#, it just might be a pain for them. If there are a lot of parameters and they logically represent something, you might want to consider encapsulating them together, in the same way that Process works with ProcessStartInfo . That's

Function Overloading in Haskell

别等时光非礼了梦想. 提交于 2019-12-04 07:15:27
I have a structure which represents the equation of a line in the form m x + b and a structure of a point Line { m :: Double, b :: Double } deriving( Show, Eq ) Point { x :: Double, y :: Double } deriving( Show, Eq ) I want the function perpendicular that does the following: perpendicular (Line m b) (Point x y) = Line m2 b2 where m2 = (-1/m) b2 = y - m2*x if given a line and a point, or a partially applied Line perpendicular (Line m b) = Line m2 where m2 = (-1/m) if only given a Line. The problem here is that I get Equations for `perpendicular' have different numbers of arguments In the first

Why is not overloaded function for derived class object invoked when given a pointer to base class in C++?

↘锁芯ラ 提交于 2019-12-04 06:18:15
问题 In the following code #include <iostream> using namespace std; class A { public: A() {} virtual ~A() {}; }; class B : public A { public: B() {} virtual ~B() {}; }; void process(const A&) { cout << "processing A" << endl; } void process(const B&) { cout << "processing B" << endl; } int main(void) { A* a = new B; process(*a); return 0; } the output of running it becomes processing A but I would have assumed that it should have been processing B since a points to the derived class B and not A .

Can I overload template variables?

ⅰ亾dé卋堺 提交于 2019-12-04 06:02:00
I want to declare something like this: template <typename T> constexpr enable_if_t<is_integral_v<T>, int[]> foo = { 1, 2 }; template <typename T> constexpr enable_if_t<is_floating_point_v<T>, int[]> foo = { 10, 20, 30 }; But when I try to I'm getting this error : error: redeclaration of template<class T> constexpr std::enable_if_t<std::is_floating_point<_Tp>::value, int []> foo note: previous declaration template<class T> constexpr std::enable_if_t<std::is_integral<_Tp>::value, int []> foo<T> I feel like this should be legal as there will never be more than one foo defined for any given