8

AFAIK, the term dispatch means just a method resolution and calling. It doesn't matter whether it is static or dynamic. I saw many people are using a term like static dispatch and dynamic dispatch.

What makes me confusing is there're also some mysterious descriptions. I was trying to understand what is multiple dispatch, and it seems just selecting a subprogram by parameter types. If I understood it correctly, there can be both of static multiple dispatch and dynamic multiple dispatch, and we can say C++ is providing multiple dispatch via free functions.

But, Wikipedia article about multiple dispatch says C++ has no multiple dispatch because it doesn't have dynamic resolution of function by multiple parameters. And I really don't get conceptual difference between Common Lisp example and C++ overloaded function. Because I can't find any conceptual difference unless the term multiple dispatch implies dynamic dispatch. And I realized that I am confusing what the dispatching really is

I also checked QA entry Multiple Dispatch vs. Function Overloading, and it seems the answer is premising the term dispatch is basically dynamic. That also makes me confusing.

What is correct meaning of the term dispatch? Does it imply dynamic resolution? Is this term well defined or just conventional? What am I missing?

4
  • 1
    "Wikipedia article about multiple dispatch says C++ has no multiple dispatch" -- which article are you referring to? C++ does have multiple dispatch. And yes, multiple dispatch does imply dynamic (see en.wikipedia.org/wiki/Multiple_dispatch)
    – miraculixx
    Commented Jan 19, 2014 at 8:34
  • @miraculixx That's the article. And the Wikipedia article don't mention about static/dynamic attribute, so I was fully confused.
    – Eonil
    Commented Jan 19, 2014 at 9:02
  • 1
    @miraculixx - C++ does not have multiple dispatch. You have to fake it with some design pattern such as the Visitor Pattern. Commented Jan 19, 2014 at 12:14
  • @David indeed, I guess I was confused there myself, and for a brief moment mixed up function overloading (which c++ does statically) and multi dispatch. errare humanum est...
    – miraculixx
    Commented Jan 19, 2014 at 13:23

3 Answers 3

12

The terms mean the following:

  • static dispatch = the dispatch order is defined at compile time. It simply means that any function/method call say foo() or x.foo() will always invoke the very same function -- this is established once and then stays that way. This implies that the compiler can determine the type of x at compile time.

  • dynamic dispatch = the dispatch order is resolved at run time. This means the compiler builds a lookup table of all functions/methods and determines which one to actually call at run time. Say there there is class A and B, both of which implement interface X with method X.bar(). At runtime, y is examined and based on its actual class either A.bar() or B.bar() is called.

  • multiple dynamic dispatch = the dispatch order is dependent on function/method name + argument types (=a.k.a. signature), and the actual implementation that gets called is determined dynamically at runtime. Say class A implements methods A.fooBar(int) and A.fooBar(char *), and there is a call to a.fooBar(x) in your program. At runtime both a and x are examined and the actual method to call is determined based on the type of x.

See Wikipedia for more on dynamic dispatch and multiple dynamic dispatch.

5
  • 1
    Can I treat C++ free functions are supporting static multiple dispatch?
    – Eonil
    Commented Jan 19, 2014 at 9:06
  • well, by definition no -- multiple dispatch implies dynamic. With c++ free functions, AFAIK the compiler does all the work which is really static dispatch which always looks at function name + signature.
    – miraculixx
    Commented Jan 19, 2014 at 9:40
  • 1
    If the term dispatch can be used on both of static and dynamic, why does multiple dispatch imply dynamic? What about static multiple dispatch?
    – Eonil
    Commented Jan 19, 2014 at 16:18
  • 1
    static dispatch means the compiler builds a lookup table of all functions, including the signature. Any static dispatch is effectively multiple dispatch, so by convention when you say multiple dispatch people will assume you imply dynamic.
    – miraculixx
    Commented Jan 19, 2014 at 16:37
  • @miraculixx Not all static dispatch is multiple dispatch. That is the case in C++ but not in general.
    – user20574
    Commented Jul 18, 2015 at 0:40
13

My advice here is: don't overthink this one. Dispatch simply means send. Dispatch an event to a listener, dispatch an interrupt to a handler, dispatch a message to a receiver, dispatch a call to a procedure or function: all aspects of the same basic concept. Send the data to the code that will handle it. Resolution means choosing between available destinations, and is just one part of dispatch.

In code terms, the dispatch starts with some kind of packet of information and something that indicate where it should be sent, and is finished when the packet has been sent (dispatched). All languages have some kind of dispatch mechanism built in, but many implement resolution and dispatching schemes to suit a unique purpose. Interrupt handling and windows message processing are examples that come to mind.

C++ can use static or dynamic resolution, but if it chooses between functions based on argument types it can only do so at compile time. Smalltalk/Objective C and Ruby resolve dispatches at runtime, as do many dynamic languages.

Single dispatch means that a single argument is thought of as the receiver and determines which method is called. The method is typically in the class for that receiver object, and the method signature is converted into an offset in a dispatch table (vtable) in that class. The privileged object in C++ is the one before the dot, which becomes the 'this' pointer.

Multiple dispatch means no privileged receiver but typically a pattern matching operation across all argument types. The Common Lisp Object System uses this approach. See https://en.wikipedia.org/wiki/Multiple_dispatch.

In C++ with overloaded operators, A+B and B+A must dispatch to different methods. In CLOS they can be the same.

I probably prefer the term MultiMethods, since it's multi-factor resolution rather than multiple dispatch per se. See http://c2.com/cgi/wiki?MultiMethods. Also http://www.codeproject.com/Articles/242749/Multiple-dispatch-and-double-dispatch.

2
  • 1
    "C++ can use static or dynamic resolution ... it can only do so at compile time", what about virtual methods? Those are determined dynamically at run-time and do depend on method signature
    – miraculixx
    Commented Jan 19, 2014 at 8:32
  • Hopefully my edit makes it clearer what I intended.
    – david.pfx
    Commented Jan 19, 2014 at 20:23
0

C++ does not have multiple (dynamic) dispatch. Consider the following:

#include <iostream>

struct Foo {
   virtual ~Foo() {}
};

struct FooOne : public Foo {};

struct Bar {
   virtual ~Bar() {}
   virtual void dispatch (const Foo &) {
      std::cout << "Bar::Dispatch(const Foo &)\n";
   }
};

struct BarOne : public Bar {
   using Bar::dispatch;
   virtual void dispatch (const Foo &) {
      std::cout << "BarOne::Dispatch(const Foo &)\n";
   }
   virtual void dispatch (const FooOne &) {
      std::cout << "BarOne::Dispatch(const FooOne &)\n";
   }
};

void process (Bar & bar, const Foo & foo) {
   bar.dispatch (foo);
}

int main () {
   Foo foo;
   Bar bar;
   FooOne foo_one;
   BarOne bar_one;

   process (bar, foo);
   process (bar, foo_one);

   process (bar_one, foo);
   process (bar_one, foo_one);

   bar_one.dispatch (foo_one);

   return 0;
}

The output from the above is

Bar::Dispatch(const Foo &)
Bar::Dispatch(const Foo &)
BarOne::Dispatch(const Foo &)
BarOne::Dispatch(const Foo &)
BarOne::Dispatch(const FooOne &)

Within process(Foo& foo, const Bar& bar), C++ uses dynamic dispatch on argument foo in the statement foo.dispatch(bar). The third and fourth output lines show this C++-style dynamic dispatch in action. The fourth output line demonstrates that C++ does not have multiple dispatch. If it did, that fourth output line would be the same as the last.

That final line? That's static dispatch. The compiler knows at compile time exactly which function needs to be called. This final call does not go through the virtual table.

1
  • The example is valid and useful, but multiple dispatch aka multimethods goes further. In a language that dispatches to a single class receiver you cannot even write the kind of example that comes naturally in CLOS.
    – david.pfx
    Commented Jan 19, 2014 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.