From 80ef143ef21b42314ad397301f8073b17718a923 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 2 Nov 2022 15:52:38 +0100 Subject: [PATCH 1/5] fixup! WIP: posts: add mutiple-dispatch-in-c++ --- content/posts/2022-06-07-multiple-dispatch-in-c++/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md b/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md index 17cdfa6..f5a307e 100644 --- a/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md +++ b/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md @@ -96,7 +96,7 @@ For example, a dummy file-system interface might look like the following: ```cpp struct Filesystem { virtual void write(std::string_view filename, std::span data) = 0; - virtual std::vector read(std::string_view filename) = 0; + virtual std::vector read(std::string_view filename) = 0; virtual void delete(std::string_view filename) = 0; }; ``` From ab2fd4bb697d5b897e0253d964878d5d8dbd5743 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 2 Nov 2022 15:52:38 +0100 Subject: [PATCH 2/5] fixup! WIP: posts: add mutiple-dispatch-in-c++ --- content/posts/2022-06-07-multiple-dispatch-in-c++/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md b/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md index f5a307e..c012b08 100644 --- a/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md +++ b/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md @@ -103,7 +103,7 @@ struct Filesystem { You can then write `PosixFilesystem` which makes use of the POSIX API and interact with actual on-disk data, `MockFilesystem` which only works in-memory -and can be used for tests only, etc... +and can be used for testing, etc... ## Double dispatch From cb9545769d61943e42c7960869253304eb35dd3b Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 2 Nov 2022 15:52:38 +0100 Subject: [PATCH 3/5] fixup! WIP: posts: add mutiple-dispatch-in-c++ --- content/posts/2022-06-07-multiple-dispatch-in-c++/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md b/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md index c012b08..902149a 100644 --- a/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md +++ b/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md @@ -105,7 +105,7 @@ You can then write `PosixFilesystem` which makes use of the POSIX API and interact with actual on-disk data, `MockFilesystem` which only works in-memory and can be used for testing, etc... -## Double dispatch +## Double dispatch through the Visitor pattern Sometimes single dispatch is not enough, such as in the collision example at the beginning of this article. In cases where a computation depends on the From 70f99f21fa76db751dd2ff476c966a9e5b7f43c4 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 2 Nov 2022 15:52:38 +0100 Subject: [PATCH 4/5] fixup! WIP: posts: add mutiple-dispatch-in-c++ --- .../posts/2022-06-07-multiple-dispatch-in-c++/index.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md b/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md index 902149a..b2377bf 100644 --- a/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md +++ b/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md @@ -107,11 +107,11 @@ and can be used for testing, etc... ## Double dispatch through the Visitor pattern -Sometimes single dispatch is not enough, such as in the collision example at -the beginning of this article. In cases where a computation depends on the -dynamic type of *two* of its values, we can make use of double-dispatch by -calling a virtual method on the first value, which will call a virtual method -on the second value. +Sometimes single dispatch is not enough, such as in the collision example at the +beginning of this article. In cases where a computation depends on the dynamic +type of *two* of its values, we can make use of double-dispatch by leveraging +the Visitor design pattern. This is done by calling a virtual method on the +first value, which itself will call a virtual method on the second value. Here's a commentated example: From 30ffd90b2a971c26f41a2f14aa52d59add3adf0b Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 2 Nov 2022 15:54:37 +0100 Subject: [PATCH 5/5] fixup! WIP: posts: add mutiple-dispatch-in-c++ --- content/posts/2022-06-07-multiple-dispatch-in-c++/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md b/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md index b2377bf..c85088e 100644 --- a/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md +++ b/content/posts/2022-06-07-multiple-dispatch-in-c++/index.md @@ -161,8 +161,8 @@ void collide(SpaceObject& first, SpaceObject& second) { }; int main() { - std::unique_ptr asteroid = std::make_unique(); - std::unique_ptr spaceship = std::make_unique(); + auto asteroid = std::make_unique(); + auto spaceship = std::make_unique(); collide(*asteroid, *spaceship); // Calls in order: