CppCon 2014: Yuriy Solodkyy "Accept No Visitors"

CppCon · 7K views · 2014 · 1:05:48

Open on YouTube

C, C++, Rust, Go & systems languagesSoftware architecture & designProgramming language designtalk

Why Stack Overflow recommends it

Stack Overflow users link it when discussing multiple dispatch, class hierarchy design and visitor boilerplate in C++, as a reference for why visitors are awkward and what pattern matching offers instead.

Recommended by 1 people across 1 different months, 2014 to 2019.

What it is

A CppCon 2014 talk by Yuriy Solodkyy, a former student of Bjarne Stroustrup who now works on Visual C++ at Microsoft. He argues the Visitor design pattern is a workaround for the lack of retroactive polymorphic functions in OO languages, and presents a Match statement as a more elegant, equally efficient alternative with several worked use cases.

What people said

Recommends Denis Yaroshevskiy · 2016-05 · Stack Overflow · on “What's the right way to overload operator== for a class hierarchy?”

I think this looks weird: void foo(const MyClass& lhs, const MyClass& rhs) { if (lhs == rhs) { MyClass tmp = rhs; // is tmp == rhs true? } } If implementing operator== seems like a legit question, consider type erasure (consider type erasure anyways, it's a lovely technique). Here is Sean Parent describing it. [LINK] Then you still have to do some multiple-dispatching. It's an unpleasant problem. Here is a talk about it. [this video] Consider using variants instead of hierarchy. They can do this type of things easyly.

Cites dyp · 2014-11 · Stack Overflow · on “Constant vs. Mutable iterators: same algorithm different code”

I think the issue is one of the issues with visitors mentioned here: cppcon 2014: accept no visitors [this video] . The underlying issue might be unrelated to iterators and visitors though; it's about having to write duplicate code for member functions due to the const-ness (and value category) of the class instance.

Cites DDG · 2019-07 · Stack Overflow · on “virtual destructor for pure abstract class”

Based on what I found here [LINK] and on other links on stackoverflow, we should always define a virtual destructor in the base class if we plan to use it polymorphically. I want to know if there is an exception to this rule. I have seen production code that does not define virtual destructor for the pure abstract base classes and in one of cppcon 2014 video Accept no visitor [this video] , around 10:06 the BoolExp struct defined is a pure abstract class and has no virtual destructor. So for a pure abstract class defined like this class Base { public: virtual foo() = 0; virtual bar() = 0; } My question is it absolutely must that we define a virtual destructor for "Base" class, even though it does