Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is visitor pattern?

197 views
Skip to first unread message

wij

unread,
Aug 20, 2023, 9:55:56 AM8/20/23
to
I found a C++ example https://refactoring.guru/design-patterns/visitor/cpp/example
but I still don't understand what it tries to say and the example code don't compile.
Can anyone explain the visitor pattern or better webpages?
probably making the example compile may help, thanks in advance.

Öö Tiib

unread,
Aug 20, 2023, 10:22:48 AM8/20/23
to
On Sunday, 20 August 2023 at 16:55:56 UTC+3, wij wrote:
> I found a C++ example https://refactoring.guru/design-patterns/visitor/cpp/example
> but I still don't understand what it tries to say and the example code don't compile.

Racoon with buzzsaw indicates self-irony.
Example that does not compile is smelly ... OTOH it perhaps just lacks #includes.

#include <array>
#include <iostream>
#include <string>

> Can anyone explain the visitor pattern or better webpages?

Maybe wikipedia?
<https://en.wikipedia.org/wiki/Visitor_pattern>

> probably making the example compile may help, thanks in advance.

Unsure ... people's mind does not grasp double dispatch too well.

Pavel

unread,
Aug 20, 2023, 2:13:01 PM8/20/23
to
This seems better than Wikipedia:

https://www.gofpattern.com/behavioral/patterns/visitor-pattern.php

Still I think both are slightly over-complicated / non-focused in how
they describe the original problem.

I would describe it follows:

PROBLEM

1. You have a collection or a stream of objects of different types thru
which you can iterate. Given [a ref to] any such object, you are able to
compute its type and/or call a virtual method.
2. You want to iterate thru these objects and do some op1 with each of
them. Op1 depends on the object type (whole or part of this dependency
could be expressible by only calling virtual method(s) on a [ref to]
common base type).
3. You will likely need to implement other similar operations, say, op2,
op3 etc

OO SOLUTION

1. Ensure all objects have a common base class
2. Add op1 (and later op2...) as a virtual method at the base class and
implement it in all most derived classes (or, if you are lucky, there
will be already some internal nodes in the hierarchy where you can plant
common logic for close siblings -- or you have to expand the hierarchy
to add such internal nodes)

OO SOLUTION analysis

Advantage: all dispatching is done for you by virtual method machinery
Drawback: need to change lots of classes every time you add one op, even
if you could implement op1,op2... using only public API of the object
type (maybe there are not many differences between how the op should
behave on the objects of all those many derived classes (but there are
some)).

VISITOR SOLUTION

1. Ensure all objects have a common base class (say, it's B)
2. create an abstract class Visitor with an abstract method doOp with an
arg of type [ref to] B.
3. add a single method to B, e.g. acceptVisit that takes [ref to]
Visitor and calls doOp on it. NOTE: only one such method is needed, no
matter how many ops you are adding.
4. for every op, create a derived class of Visitor (e.g. Op1Visitor,
Op2Visitor) e.g. and implement doOp() method.

VISITOR SOLUTION analysis:

Advantages:
1. For every new op, you have to add only one class with one virtual method
2. All logic pertaining to the new op is in one place (doOp method).
Drawbacks:
1. There could be need in some dispatching logic in doOp() method
2. All doOp() methods may have to change if a new type is added

EXAMPLE

Enhance formatting of an XML document by adding a red border around the
current depiction of only some elements, depending on the element type.

CONCLUSION

Consider using Visitor when the hierarchy of object types is stable and
an op to add is not a natural extension of the type API that you would
want to implement anyway.

HTH
-Pavel

Öö Tiib

unread,
Aug 20, 2023, 4:34:49 PM8/20/23
to
That is illustration of why I said "people's mind does not grasp double
dispatch too well."

> 1. Ensure all objects have a common base class (say, it's B)

Not needed.

> 2. create an abstract class Visitor with an abstract method doOp with an
> arg of type [ref to] B.

Nope, pile of overloads of doOp one for each type processed. Otherwise
how you process different types differently?

> 3. add a single method to B, e.g. acceptVisit that takes [ref to]
> Visitor and calls doOp on it. NOTE: only one such method is needed, no
> matter how many ops you are adding.

Not enough. Every type processed has to have that copy-paste
acceptVisit method. Otherwise double dispatch does not work.

Pavel

unread,
Aug 20, 2023, 6:02:21 PM8/20/23
to
This would render the pattern useless as has no advantage over adding a
virtual method to do the op. The rationale of the pattern is to permit
coding one op in one place.

> Otherwise
> how you process different types differently?
I explained this in the problem statement. "Given [a ref to] any such
object, you are able to
compute its type and/or call a virtual method". In practice this can be
anything, examples (non-exhaustive): a enum type discriminator, virtual
cast method, (in modern languages), pattern matching.

>
>> 3. add a single method to B, e.g. acceptVisit that takes [ref to]
>> Visitor and calls doOp on it. NOTE: only one such method is needed, no
>> matter how many ops you are adding.
>
> Not enough. Every type processed has to have that copy-paste
> acceptVisit method. Otherwise double dispatch does not work.
Double dispatch works just fine without copy-pasting. Dispatch #1 (by
the operation type) is done by the virtual method doOp. Dispatch #2 (by
the object type) is done by using the actual type to call specific
methods and/or calling virtual methods, on the object ref.

Alf P. Steinbach

unread,
Aug 20, 2023, 7:18:00 PM8/20/23
to
#include <fmt/core.h> // <url: https://github.com/fmtlib/fmt> tip:
define FMT_HEADER_ONLY

#include <variant>
#include <vector>

#include <math.h>

namespace cppm {
template< class Type > using in_ = const Type&;
} // namespace cppm

namespace geometry {
struct Point { double x; double y; };
struct Distance { double dx; double dy; };
struct Rect_size { double w; double h; };

constexpr auto operator+( const Point pt, const Distance d )
-> Point
{ return Point{ pt.x + d.dx, pt.y + d.dy }; }

constexpr auto operator-( const Point pt, const Distance d )
-> Point
{ return Point{ pt.x - d.dx, pt.y - d.dy }; }
} // namespace geometry

namespace math{
#ifndef M_PI
# error "Define `_USE_MATH_DEFINES` in the build to get Posix
constants like `M_PI`."
#endif
const double pi = M_PI;

template< class Number >
constexpr auto square_of( const Number x ) -> Number { return x*x; }
} // namespace math

namespace shape {
using geometry::Point, geometry::Distance, geometry::Rect_size;
using math::pi, math::square_of;

class Rectangle;

class Shape
{
Point m_lower_left;

protected:
Shape( const Point pt = {} ): m_lower_left( pt ) {}

public:
virtual ~Shape() {}

virtual auto area() const -> double = 0;

auto bb_pos() const -> Point { return
m_lower_left; }
virtual auto bb_size() const -> Rect_size = 0;
inline auto bounding_box() const -> Rectangle;
};

class Rectangle:
public Shape
{
Rect_size m_size;

public:
Rectangle(): Shape(), m_size() {}
Rectangle( const Point ll, const Rect_size size ): Shape( ll ),
m_size( size ) {}

auto area() const -> double { auto& _ = m_size; return
_.w*_.h; }
auto bb_size() const -> Rect_size override { return m_size; }
};

inline auto Shape::bounding_box() const -> Rectangle { return
Rectangle{ bb_pos(), bb_size() }; }

class Circle: public Shape
{
double m_radius;

public:
Circle(): Shape(), m_radius() {}

Circle( const Point center, double radius ):
Shape( center - Distance{ radius, radius } ),
m_radius( radius )
{}

auto radius() const -> double { return m_radius; }
auto diameter() const -> double { return 2*radius(); }
auto center() const -> Point { return bb_pos() +
Distance{ radius(), radius() }; }

auto area() const -> double { return pi*square_of(
m_radius ); }
auto bb_size() const -> Rect_size override { return
Rect_size{ diameter(), diameter() }; }
};

} // namespace shape

namespace scene {
using cppm::in_;
using shape::Shape, shape::Rectangle, shape::Circle;
using std::get, std::variant, std::visit, // <variant>
std::vector; // <vector>

class Shape_holder
{
Shape* m_p_shape;
variant<Rectangle, Circle> m_shape;

void set_pointer() { visit( [&]( auto& r ) { m_p_shape = &r; },
m_shape ); }

public:
template< class Concrete_shape >
Shape_holder( Concrete_shape shape ): m_shape( shape ) {
set_pointer(); }

Shape_holder( in_<Shape_holder> other ): m_shape( other.m_shape
) { set_pointer(); }

auto shape() -> Shape& { return *m_p_shape; }

template< class Visitor >
void pass_shape_to( Visitor&& visitor ) { visit( visitor,
m_shape ); }

template< class Visitor >
void pass_shape_to( Visitor&& visitor ) const { visit( visitor,
m_shape ); }
};

using Shapes = vector<Shape_holder>;
} // scene

auto main() -> int
{
using cppm::in_;
using geometry::Rect_size;
using scene::Shapes;
using shape::Rectangle, shape::Circle;

Shapes shapes = { Rectangle( {}, {1, 2} ), Circle( {}, 3 ) };
for( auto& holder: shapes ) {
struct Visitor
{
void operator()( in_<Rectangle> rect ) const
{
const Rect_size bb = rect.bb_size();
fmt::print( "Rectangle {}×{}.\n", bb.w, bb.h );
}

void operator()( in_<Circle> circle ) const
{
fmt::print( "Circle with radius {}.\n", circle.radius()
); // Only avail in Circle.
}
};

fmt::print( "Area {} in a ", holder.shape().area() ); //
Virtual call.
holder.pass_shape_to( Visitor() ); //
Visitation.
}
}

- Alf

Alf P. Steinbach

unread,
Aug 20, 2023, 7:24:28 PM8/20/23
to
On 2023-08-21 1:17 AM, Alf P. Steinbach wrote:
[snip code]

Some imprefections because I just wrote that and posted it. Then looked
at the posting and noticed should e.g. take care of assignment op. It's
the news-client's fault for providing a too easy to click "post" button.

- Alf

wij

unread,
Aug 20, 2023, 10:24:25 PM8/20/23
to
Thank. When I saw "array" I habitually translate to "vector", so the 2nd
parameter in "std::array<const Component *, 2>" looked strange to me!!!

From the compiled example, it looked to me that 'Visitor pattern' simply means
a class family (Component) has a virtual member, say Accept(..) in the 'visitor'
case, whose definition is defined by another class member (e.g. Visit::xxx)... no more.

Öö Tiib

unread,
Aug 21, 2023, 12:12:52 AM8/21/23
to
On Monday, 21 August 2023 at 01:02:21 UTC+3, Pavel wrote:
> Öö Tiib wrote:
> > On Sunday, 20 August 2023 at 21:13:01 UTC+3, Pavel wrote:
> >
> >> 1. Ensure all objects have a common base class (say, it's B)
> >
> > Not needed.
> >
> >> 2. create an abstract class Visitor with an abstract method doOp with an
> >> arg of type [ref to] B.
> >
> > Nope, pile of overloads of doOp one for each type processed.
>
> This would render the pattern useless as has no advantage over adding a
> virtual method to do the op. The rationale of the pattern is to permit
> coding one op in one place.
>
The Visitor class is that "one place"; doing same operation lets say printing
of 50 types in one function results with huge function that is headache
not advantage. Separate functions are testable and maintainable.

> > Otherwise
> > how you process different types differently?
> I explained this in the problem statement. "Given [a ref to] any such
> object, you are able to
> compute its type and/or call a virtual method".

IOW you do huge switch typeid ... case static_ cast or have the virtual
method (over what we were "gaining advantage") already there?
But we need no bloat from that pattern to do neither. These were the
things we wanted to get rid of.

> In practice this can be
> anything, examples (non-exhaustive): a enum type discriminator, virtual
> cast method, (in modern languages), pattern matching.
> >
> >> 3. add a single method to B, e.g. acceptVisit that takes [ref to]
> >> Visitor and calls doOp on it. NOTE: only one such method is needed, no
> >> matter how many ops you are adding.
> >
> > Not enough. Every type processed has to have that copy-paste
> > acceptVisit method. Otherwise double dispatch does not work.
>
> Double dispatch works just fine without copy-pasting. Dispatch #1 (by
> the operation type) is done by the virtual method doOp. Dispatch #2 (by
> the object type) is done by using the actual type to call specific
> methods and/or calling virtual methods, on the object ref.

Of course huge switch or calling virtual method (or now even multiple)
work without the whole pattern. Read up on "double dispatch".
It is tricky like I already said.

Bonita Montero

unread,
Aug 21, 2023, 12:18:43 AM8/21/23
to
You can better do this with functional programming. If you need
a performant solution you'd pass a function-object of a generic
type. If performance is not your concern or not necessary you'd
pass a function<>-object.
Take this example of a file-listing function for Win32:

void fileList( wchar_t const *path, function<bool ( WIN32_FIND_DATAW
const & )> const &fn, DWORD *pdwErr )
{
using namespace std;
if( pdwErr )
*pdwErr = NO_ERROR;
wstring fullPath = getAbsolutePath( path );
WIN32_FIND_DATAW fd;
HANDLE hFind = FindFirstFileW( fullPath.c_str(), &fd );
if( hFind == INVALID_HANDLE_VALUE )
{
if( GetLastError() == ERROR_FILE_NOT_FOUND )
return;
if( !pdwErr )
throw system_error( (int)GetLastError(), system_category(),
"FindFirstFileW() failed" );
*pdwErr = GetLastError();
return;
}
invoke_on_destruct closeFind(
[&]()
{
BOOL succ = FindClose( hFind );
assert(succ);
} );
wstring dir( extractDirectory( fullPath.c_str(), false ) );
do
if( wcscmp( fd.cFileName, L"." ) != 0 && wcscmp( fd.cFileName, L".." )
!= 0 && !fn( fd ) )
break;
while( FindNextFileW( hFind, &fd ) );
if( GetLastError() != ERROR_NO_MORE_FILES )
{
if( !pdwErr )
throw system_error( (int)GetLastError(), system_category(),
"FindNextFileW() failed" );
*pdwErr = GetLastError();
}
}

Mut...@dastardlyhq.com

unread,
Aug 21, 2023, 2:53:20 AM8/21/23
to
On Sun, 20 Aug 2023 21:12:41 -0700 (PDT)
=?UTF-8?B?w5bDtiBUaWli?= <oot...@hot.ee> wrote:
>On Monday, 21 August 2023 at 01:02:21 UTC+3, Pavel wrote:
>> object, you are able to
>> compute its type and/or call a virtual method".=20
>
>IOW you do huge switch typeid ... case static_ cast or have the virtual=20
>method (over what we were "gaining advantage") already there?
>But we need no bloat from that pattern to do neither. These were the
>things we wanted to get rid of.=20

IME most design patterns are algorithms any half decent programmer could
have thought up by themselves and/or have been around for half a century
already but they simply got repackaged for a new generation.

Old wine -> new bottle.


Öö Tiib

unread,
Aug 21, 2023, 3:10:52 AM8/21/23
to
On Monday, 21 August 2023 at 09:53:20 UTC+3, Mut...@dastardlyhq.com wrote:
> On Sun, 20 Aug 2023 21:12:41 -0700 (PDT)
> =Öö Tiib <oot...@hot.ee> wrote:
> >On Monday, 21 August 2023 at 01:02:21 UTC+3, Pavel wrote:
> >> object, you are able to
> >> compute its type and/or call a virtual method".
> >
> >IOW you do huge switch typeid ... case static_ cast or have the virtual
> >method (over what we were "gaining advantage") already there?
> >But we need no bloat from that pattern to do neither. These were the
> >things we wanted to get rid of.
>
> IME most design patterns are algorithms any half decent programmer could
> have thought up by themselves and/or have been around for half a century
> already but they simply got repackaged for a new generation.
>
> Old wine -> new bottle.
>
Double dispatch was first perhaps described by Dan Ingalls in seventies in
context of SmallTalk ... so you are correct that half a century old.
There seems to be no difference how to package, too lot of people do not
get the idea of it (but can be assigned to maintain) and result is either
broken product or the nuisance what it got rid of edited back in.

Pavel

unread,
Aug 21, 2023, 10:55:32 PM8/21/23
to
Öö Tiib wrote:
> On Monday, 21 August 2023 at 01:02:21 UTC+3, Pavel wrote:
>> Öö Tiib wrote:
>>> On Sunday, 20 August 2023 at 21:13:01 UTC+3, Pavel wrote:
>>>
>>>> 1. Ensure all objects have a common base class (say, it's B)
>>>
>>> Not needed.
>>>
>>>> 2. create an abstract class Visitor with an abstract method doOp with an
>>>> arg of type [ref to] B.
>>>
>>> Nope, pile of overloads of doOp one for each type processed.
>>
>> This would render the pattern useless as has no advantage over adding a
>> virtual method to do the op. The rationale of the pattern is to permit
>> coding one op in one place.
>>
> The Visitor class is that "one place"; doing same operation lets say printing
> of 50 types in one function results with huge function that is headache
> not advantage. Separate functions are testable and maintainable.
>
>>> Otherwise
>>> how you process different types differently?
>> I explained this in the problem statement. "Given [a ref to] any such
>> object, you are able to
>> compute its type and/or call a virtual method".
>
> IOW you do huge switch typeid ... case static_ cast or have the virtual
> method (over what we were "gaining advantage") already there?
Even a switch with 30 cases is no worse than 30 methods although switch
is not always necessary (try to read the above instead of imagining).

The advantage is in having this logic in one place. If your visitor does
a coherent thing (which it is supposed to do; else why is it a single
visitor, there could be and usually would be common code for all or many
visited). The most pliant cases (that are exactly those gaining most
from applying a visitor pattern) may get by object virtual methods.
Simple example:

class BorderingVisitor: public AbstractVisitor {
public:
void doOp(const Shape& s) const override {
if (s.area() > 25) {
dc.drawRedRectangle(s.boundingRectangle().increaseBy(0.1));
}
}
private:
DrawingContext dc;
};

If we wanted a green border around a shape of any of 10 classes derived
from convex polygon (ConvexTriangle, ConvexQuadrilateral etc), red
border around a shape of any of 10 classes derived from Oval and no
border around any of 30 other zero-area shape classes, all we need to do
is to change doOp to:

void BorderingVisitor::doOp(const Shape& s) const {
if (s.area() > 25) {
const Rectangle br{s.boundingRectangle().increaseBy(0.1)};
if (const ConvexPolygon* cp = s.castToConvexPolygon()) {
dc.drawGreenRectangle(br);
}
else {
assert(!!s.castToOval());
dc.drawRedRectangle(br);
}
}
}

And this is the complete code for adding a new operation (virtual cast
is to be only defined once for all operations)

> But we need no bloat from that pattern to do neither. These were the
> things we wanted to get rid of.

Care to show us the code for your "non bloated" "one-place" visitor
class for the above 2 problems? (like I did, you can omit the code that
is common for all operations).

>
>> In practice this can be
>> anything, examples (non-exhaustive): a enum type discriminator, virtual
>> cast method, (in modern languages), pattern matching.
>>>
>>>> 3. add a single method to B, e.g. acceptVisit that takes [ref to]
>>>> Visitor and calls doOp on it. NOTE: only one such method is needed, no
>>>> matter how many ops you are adding.
>>>
>>> Not enough. Every type processed has to have that copy-paste
>>> acceptVisit method. Otherwise double dispatch does not work.
>>
>> Double dispatch works just fine without copy-pasting. Dispatch #1 (by
>> the operation type) is done by the virtual method doOp. Dispatch #2 (by
>> the object type) is done by using the actual type to call specific
>> methods and/or calling virtual methods, on the object ref.
>
> Of course huge switch or calling virtual method (or now even multiple)
> work without the whole pattern. Read up on "double dispatch".
> It is tricky like I already said.
>
Surely you are the most competent software engineer understanding
multiple dispatch. Care to point out a "huge switch" in either of the
above 2 examples?

Pavel

unread,
Aug 21, 2023, 11:28:48 PM8/21/23
to
Bonita Montero wrote:
> You can better do this
I suspect we are talking about different "this"es. A design pattern is a
recipe for solving a problem of a defined class. What is the class of
problems that your code intends to demonstrate how to solve?

Bonita Montero

unread,
Aug 21, 2023, 11:47:45 PM8/21/23
to
Am 22.08.2023 um 05:28 schrieb Pavel:
> Bonita Montero wrote:
>> You can better do this
> I suspect we are talking about different "this"es. A design pattern is
> a recipe for solving a problem of a defined class. What is the class of
> problems that your code intends to demonstrate how to solve?

Visitor pattern and inversion of control is basically the same.

Pavel

unread,
Aug 22, 2023, 12:11:39 AM8/22/23
to
Bonita Montero wrote:
> Am 22.08.2023 um 05:28 schrieb Pavel:
>> Bonita Montero wrote:
>>> You can better do this
>> I suspect we are talking about different "this"es. A design pattern is
>> a recipe for solving a problem of a defined class. What is the class
>> of problems that your code intends to demonstrate how to solve?
>
> Visitor pattern and inversion of control is basically the same.
What represents the abstract element and concrete elements of different
types derived from the abstract element -- all of which are the
mandatory participants of a Visitor problem -- in your code?

Öö Tiib

unread,
Aug 22, 2023, 12:57:06 AM8/22/23
to
Visitor is typically used to search, filter, draw or print whole data object
hierarchy, convert to JSON to XML or to tree in GUI. If whole data hierarchy
is small then you get 30 cases. Switch case is worse than virtual methods.
Think why virtual methods were added? To get rid of switch cases over type.
Visitor is not meant as excuse to add those back, otherwise just use
the switch case in function, do not manufacture visitors that do not use
double dispatch for confusion.

> The advantage is in having this logic in one place. If your visitor does
> a coherent thing (which it is supposed to do; else why is it a single
> visitor, there could be and usually would be common code for all or many
> visited). The most pliant cases (that are exactly those gaining most
> from applying a visitor pattern) may get by object virtual methods.
> Simple example:
>
> If we wanted a green border around a shape of any of 10 classes derived
> from convex polygon (ConvexTriangle, ConvexQuadrilateral etc), red
> border around a shape of any of 10 classes derived from Oval and no
> border around any of 30 other zero-area shape classes, all we need to do
> is to change doOp to:
>

Unused cp in your code?
Can't post sane code from gg ... but also don't want to install newsreaders
to all devices I use. It is drawing border to any shape and so it is unclear
why you need visitor. Logical one place is either DrwawingContext's or
Shape's nonvirtual method or free function:

void Shape::draw_border(DrawingContext &dc) const {
if (area() <= 25) return;
const Rectangle br{boundingRectangle().increaseBy(0.1)};
if (isConvexPolygon()) { dc.drawGreenRectangle(br); return; }
assert(isOval());
dc.drawRedRectangle(br);
}

>
> And this is the complete code for adding a new operation (virtual cast
> is to be only defined once for all operations)

You forget that you have BorderingVisitor class of unclear life , inject
DrawingContext to it (is it copy?), etc. result is

BorderingVisitor bv(dc);
s.acceptVisit(bv);

instead one of:

dc.draw_border(s);

s.draw_border(dc);

draw_border(dc, s);

> > But we need no bloat from that pattern to do neither. These were the
> > things we wanted to get rid of.
> Care to show us the code for your "non bloated" "one-place" visitor
> class for the above 2 problems? (like I did, you can omit the code that
> is common for all operations).

Yes, sorry for gg breaking all formating.
> >
> >> In practice this can be
> >> anything, examples (non-exhaustive): a enum type discriminator, virtual
> >> cast method, (in modern languages), pattern matching.
> >>>
> >>>> 3. add a single method to B, e.g. acceptVisit that takes [ref to]
> >>>> Visitor and calls doOp on it. NOTE: only one such method is needed, no
> >>>> matter how many ops you are adding.
> >>>
> >>> Not enough. Every type processed has to have that copy-paste
> >>> acceptVisit method. Otherwise double dispatch does not work.
> >>
> >> Double dispatch works just fine without copy-pasting. Dispatch #1 (by
> >> the operation type) is done by the virtual method doOp. Dispatch #2 (by
> >> the object type) is done by using the actual type to call specific
> >> methods and/or calling virtual methods, on the object ref.
> >
> > Of course huge switch or calling virtual method (or now even multiple)
> > work without the whole pattern. Read up on "double dispatch".
> > It is tricky like I already said.
> >
> Surely you are the most competent software engineer understanding
> multiple dispatch. Care to point out a "huge switch" in either of the
> above 2 examples?

There are no double dispatch in your code so the whole acceptVisit, doOp
add nothing. These were entirely added to do double dispatch to add
virtual methods to large potentially unrelated data hierarchies without
changing any of classes in those hierarchies. What you do is not even
use case for visitor pattern.

Bonita Montero

unread,
Aug 22, 2023, 8:30:49 AM8/22/23
to
Am 22.08.2023 um 06:11 schrieb Pavel:
> Bonita Montero wrote:
>> Am 22.08.2023 um 05:28 schrieb Pavel:
>>> Bonita Montero wrote:
>>>> You can better do this
>>> I suspect we are talking about different "this"es. A design pattern is
>>> a recipe for solving a problem of a defined class. What is the class
>>> of problems that your code intends to demonstrate how to solve?
>>
>> Visitor pattern and inversion of control is basically the same.
> What represents the abstract element and concrete elements of different
> types derived from the abstract element -- all of which are the
> mandatory participants of a Visitor problem -- in your code?

My code and the visitor-pattern have in common that
there's some callback with its own state, i.e. both
have some inversion of control

Pavel

unread,
Aug 24, 2023, 1:39:04 AM8/24/23
to
correct

> If whole data hierarchy
> is small then you get 30 cases. Switch case is worse than virtual methods.
*In general* neither is better or worse. In particular, as I explained
at the beginning, if the operation is decomposed correctly, its behavior
on the objects of different types has to be coherent, similar in some
way (and maybe more similar than different) and, as such, a significant
parts of the context or results of intermediate computations could be
shared. I demonstrated this in my example with red and green borders: a)
a condition by area prevents visitor from operating on objects is
computed from their property that is orthogonal to their dynamic type;
and b) the exact behavior of the operation (red vs green border in the
example) depends on a subset of types (which, in general, is not
necessarily compact in the hierarchy).

> Think why virtual methods were added? To get rid of switch cases over type.
Of course, not. Virtual methods were *used*, with less or greater
success, to replace switch statements in event-processing scenarios
(like window behavior) (with greater success) or in state machines (with
lesser success).

But, they were *invented* to implement polymorphic behavior and
relatively loose coupling (generally, lesser dependencies) between the
code that calls the method and the code that implements the method.

Obviously, a single switch statement couples everything together and the
compilation unit where the switch is depends on every piece of code and
every concrete type the switch statement handles -- to the extent its
behavior in the operation must differ from that of other types). But
this loose coupling is only desirable when the behaviors are severely
different. On the other hand, when the operation does *to significant
extent, a single thing* on different type, with 2-3 relatively small
type-dependent differences, these differences are often better expressed
in if statement, switch or in a number of different ways (including
calling combinations of virtual functions *from within the top-level
method implementing the operation*)

> Visitor is not meant as excuse to add those back, otherwise just use
> the switch case in function, do not manufacture visitors that do not use
> double dispatch for confusion.
Ohh. Not again. If you want to use virtual functions, use them, you
don't need visitor for this. Add virtual void newOperation(context
parameters) to the base class and call it in some for_each. This is
exactly what I explained in the first post: Visitor itself is an
alternative to adding a virtual methods. The criteria which one to
choose are well known from the original works on Visitor, like GoF etc:
if your type hierarchy is big, stable, its model is well understood and
established (like GUI toolkit), and hence you are unlikely to change the
hierarchy often BUT you need to add new operations often, use visitors
(because you don't need to add thing to every class for every
operation). If your type hierarchy is dynamic and is expected to grow
often but the set of operation is stable, well establish, unlikely to
change often (like unit actions in a strategy game where you add and
enhance units but all they are going to do is to essentially move, shoot
and die), add a virtual method.

The first thing to explain about Visitor (or any other design pattern)
is how to decide when to use it and when not to use it (the scope).

If the decision to use Visitor is correct, we already know the class
hierarchy is stable and there is no harm for the code to depend on a
fixed set of these classes. The double dispatch for Visitor is just a
tool to dispatch to the code specific to the type after you dispatched
to the specific operation. Nothing more, nothing less, see definition of
multiple dispatch. The specific way to double-dispatch that many Visitor
examples (unfortunately, started with GoF book, and shown in Wikipedia)
show, is simply the *implementation detail*. You do not have to trust me.

Says Wikipedia:

"In software engineering, double dispatch is a special form of multiple
dispatch, and a mechanism that dispatches a function call to different
concrete functions depending on the runtime types of two objects
involved in the call"

And, looking at same Wikipedia on multiple dispatch, C++ is *not* one of
the languages that natively support multiple dispatch and there are
multiple ways of implementing it, among them
- "using run time type comparison via dynamic_cast" (if statement method)
- "pointer-to-method lookup table"; and (misleadingly)
- "the visitor pattern"

Why "misleadingly"? Because, looking at visitor pattern, you find

"The visitor takes the instance reference as input, and implements the
goal through double dispatch."

So, according to Wikipedia article on multiple dispatch one can
implement it (among the others) using visitor pattern, whereas visitor
pattern "implements the goal via double dispatch" whereas the double
dispatch is a special form of multiple dispatch.

Some readers (which seem to include you) resolve this circular reference
ad-hoc: they see that *specific implementation* of multiple dispatch in
C++ that happens to illustrate the Visitor article (stemming from the
GoF book, unfortunately) and somehow decide that this is *the right way*
(TM) to implement double dispatch. This is despite:

- that double dispatch is defined as a concept an there is no *the right
way* of doing it in C++ and Wikipedia itself illustrates two such ways
(both different from the one used in the visitor article and GoF book)
in the article on multiple dispatch.

- that it must be obvious that it is Visitor that uses double dispatch
in its implementation, not other may around, because the part cannot use
the whole and Visitor is "bigger" than double-dispatch because it uses
both double dispatch and iteration (through the objects in a data
structure) but iteration is not a part of double dispatch.

The problem with that (by and large, randomly-selected) implementation
of double dispatch is that it is a bad choice to illustrate Visitor
pattern specifically. Besides the mere distracting the reader to its
clever *implementation details* (not essential for understanding the
Visitor pattern), it also makes it difficult to emphasize the coherence
and cohesiveness of the newly added operation and does nothing to
explain how to organize the access of the new operation to all pieces of
data it may need. Taking into account that the whole Visitor pattern is
about adding a new operation to working on a collection or stream ("the
structure") of objects, it is a very unfortunate choice indeed.

>
>> The advantage is in having this logic in one place. If your visitor does
>> a coherent thing (which it is supposed to do; else why is it a single
>> visitor, there could be and usually would be common code for all or many
>> visited). The most pliant cases (that are exactly those gaining most
>> from applying a visitor pattern) may get by object virtual methods.
>> Simple example:
>>
>> If we wanted a green border around a shape of any of 10 classes derived
>> from convex polygon (ConvexTriangle, ConvexQuadrilateral etc), red
>> border around a shape of any of 10 classes derived from Oval and no
>> border around any of 30 other zero-area shape classes, all we need to do
>> is to change doOp to:
>>
>
> Unused cp in your code?
Yes, I started with taking bounding rectangle from every type separately
(which sometimes may be a valid micro-optimization if we use a fully
qualified non-virtual call; but then decided to keep it simple to
emphasize the points I wanted to emphasize)

> Can't post sane code from gg ... but also don't want to install newsreaders
> to all devices I use. It is drawing border to any shape and so it is unclear
> why you need visitor. Logical one place is either DrwawingContext's or
> Shape's nonvirtual method or free function:
>
> void Shape::draw_border(DrawingContext &dc) const {
> if (area() <= 25) return;
> const Rectangle br{boundingRectangle().increaseBy(0.1)};
> if (isConvexPolygon()) { dc.drawGreenRectangle(br); return; }
> assert(isOval());
> dc.drawRedRectangle(br);
> }
exactly, if you are ok to change the hierarchy every time you add an op,
you might not need the visitor, all you need is to call draw_border from
some for_each loop or high-order function.

This might be a valid choice sometimes (my first post started with
this), but you pay by changing every class *every time you need a new
operation* and making all Shape classes dependent on DrawingContext (and
other contexts you might need for different operations (e.g. log file,
network socket, database connection etc)). In visitor pattern, on the
other hand, the concrete visitor is the only code that becomes dependent
on that op-specific context.
Yes it is, see above (or directly Wikipedia article on multiple dispatch
for the examples on how Double dispatch can be implemented in C++).

> These
Unsure I understand what "these" refers to, will assume my doOp and
acceptVisit functions

> were entirely added to do double dispatch
correct. acceptVisit dispatches by op (the 1st parameter of the outer
structure "visit" operation) and doOp dispatches by object type (in my
examples, either by using its base type's virtual function only or via a
combination of the same and `if' statement on virtual cast result)

> to add
> virtual methods to large potentially unrelated data hierarchies without
> changing any of classes in those hierarchies.
I assume, by "potentially unrelated" data hierarchy, you mean the new op
has to behave entirely differently on every type of the hierarchy and
does not need common data. The issue with this assumption is that the
very first op you add might relate to the classes like this with but the
second might benefit from exploiting the type similarities or grouping
them differently (not even necessary in groups compact in the hierarchy
graph).


> What you do is not even
> use case for visitor pattern.
Says Wikipedia:

"
What problems can the Visitor design pattern solve?

It should be possible to define a new operation for (some) classes of an
object structure without changing the classes.
"

"What I do" achieves the above IMO, so it is definitely the use case.
You might argue whether or not my solution is a Visitor pattern (it is,
at least it includes all canonical Visitor participants (Abstract
Visitor, Concrete Visitors, Abstract Element, Concrete Elements (an
object structure providing access to the elements is implied)) and none
extra) but I don't understand how it can be argued that my code does not
define a new operation for (some) classes of an object structure. And it
surely does not change the classes.

Pavel

unread,
Aug 24, 2023, 1:56:52 AM8/24/23
to
Bonita Montero wrote:
> Am 22.08.2023 um 06:11 schrieb Pavel:
>> Bonita Montero wrote:
>>> Am 22.08.2023 um 05:28 schrieb Pavel:
>>>> Bonita Montero wrote:
>>>>> You can better do this
>>>> I suspect we are talking about different "this"es. A design pattern is
>>>> a recipe for solving a problem of a defined class. What is the class
>>>> of problems that your code intends to demonstrate how to solve?
>>>
>>> Visitor pattern and inversion of control is basically the same.
>> What represents the abstract element and concrete elements of
>> different types derived from the abstract element -- all of which are
>> the mandatory participants of a Visitor problem -- in your code?
>
> My code and the visitor-pattern have in common that
> there's some callback with its own state, i.e. both
> have some inversion of control
IoC is very generic so a Visitor-pattern *framework* library can likely
be characterized as IoC; but then in your code the "template method" of
the framework (fileList) does not work on an object of type from some
non-trivial type hierarchy which is where the Visitor can be applied. Of
course you can argue that the type WIN32_FIND_DATAW represents a
degenerative hierarchy of one type but Visitor is not useful for working
on degenerative hierarchies. Your code is some custom framework to
iterate thru files; could be even useful but one cannot "do better" in
solving a problem by given class by solving some random problem of
entirely different class.

Bonita Montero

unread,
Aug 24, 2023, 3:16:17 AM8/24/23
to
Am 24.08.2023 um 07:56 schrieb Pavel:

> IoC is very generic so a Visitor-pattern *framework* library can likely
> be characterized as IoC; but then in your code the "template method" of
> the framework (fileList) does not work on an object of type from some
> non-trivial type hierarchy which is where the Visitor can be applied.
> Of course you can argue that the type WIN32_FIND_DATAW represents a
> degenerative hierarchy of one type but Visitor is not useful for
> working on degenerative hierarchies. Your code is some custom
> framework to iterate thru files; could be even useful but one
> cannot "do better" in solving a problem by given class by solving
> some random problem of entirely different class.

The visitor pattern is a IoC-pattern, but not every IoC-pattern is a
visitor pattern. I like functional programming and I think that the
visitor pattern can be enhanced in this way, making it more IoC-ish.

Öö Tiib

unread,
Aug 24, 2023, 7:45:25 AM8/24/23
to
Switch case over type is in general worse than virtual functions. Longer
function, bigger cyclomatic complexity, harder to understand, harder to
form confidence that it is valid, harder to maintain, harder to test and
also may need more access to implementation details of all types
involved, breaking encapsulation.

> In particular, as I explained
> at the beginning, if the operation is decomposed correctly, its behavior
> on the objects of different types has to be coherent, similar in some
> way (and maybe more similar than different) and, as such, a significant
> parts of the context or results of intermediate computations could be
> shared.

Translating data hierarchy to xml for particular version of particular
protocol while supporting several such in parallel? There can be
intermediate computations shared but all are typically in in visitor
classes or helpers of those. No one wants the classes in hierarchy
to know anything about xml whatsoever, nothing to talk about
particular brand used in particular version of kinds of end-points.

> I demonstrated this in my example with red and green borders: a)
> a condition by area prevents visitor from operating on objects is
> computed from their property that is orthogonal to their dynamic type;
> and b) the exact behavior of the operation (red vs green border in the
> example) depends on a subset of types (which, in general, is not
> necessarily compact in the hierarchy).
>
Your example did not need visitor pattern.

> > Think why virtual methods were added? To get rid of switch cases over type.
> Of course, not. Virtual methods were *used*, with less or greater
> success, to replace switch statements in event-processing scenarios
> (like window behavior) (with greater success) or in state machines (with
> lesser success).
>
> But, they were *invented* to implement polymorphic behavior and
> relatively loose coupling (generally, lesser dependencies) between the
> code that calls the method and the code that implements the method.
>
Same loose coupling is with code that calls function that does switch
case over type, so that kind of victory wasn't achieved.

> Obviously, a single switch statement couples everything together and the
> compilation unit where the switch is depends on every piece of code and
> every concrete type the switch statement handles -- to the extent its
> behavior in the operation must differ from that of other types). But
> this loose coupling is only desirable when the behaviors are severely
> different.
>
I don't follow what you are saying. Different stock markets and other
financial institutions for example are not severely different in essence,
but the format of data you are expected to exchange with their servers
differs massively enough.

> On the other hand, when the operation does *to significant
> extent, a single thing* on different type, with 2-3 relatively small
> type-dependent differences, these differences are often better expressed
> in if statement, switch or in a number of different ways (including
> calling combinations of virtual functions *from within the top-level
> method implementing the operation*)
>
That is not use-case of visitor pattern.

> > Visitor is not meant as excuse to add those back, otherwise just use
> > the switch case in function, do not manufacture visitors that do not use
> > double dispatch for confusion.
>
> Ohh. Not again. If you want to use virtual functions, use them, you
> don't need visitor for this. Add virtual void newOperation(context
> parameters) to the base class and call it in some for_each. This is
> exactly what I explained in the first post: Visitor itself is an
> alternative to adding a virtual methods.
>
You add same number of virtual methods of visitor if you need several
different classes of visitors to visit over same data hierarchies. The
gain is that these are in one place, in particular visitor and that the
classes in hierarchies are not changed by single letter.

> The criteria which one to
> choose are well known from the original works on Visitor, like GoF etc:
> if your type hierarchy is big, stable, its model is well understood and
> established (like GUI toolkit), and hence you are unlikely to change the
> hierarchy often BUT you need to add new operations often, use visitors
> (because you don't need to add thing to every class for every
> operation). If your type hierarchy is dynamic and is expected to grow
> often but the set of operation is stable, well establish, unlikely to
> change often (like unit actions in a strategy game where you add and
> enhance units but all they are going to do is to essentially move, shoot
> and die), add a virtual method.
>
> The first thing to explain about Visitor (or any other design pattern)
> is how to decide when to use it and when not to use it (the scope).
>
That is true, but you seem to suggest to misuse it, where unneeded.

> If the decision to use Visitor is correct, we already know the class
> hierarchy is stable and there is no harm for the code to depend on a
> fixed set of these classes. The double dispatch for Visitor is just a
> tool to dispatch to the code specific to the type after you dispatched
> to the specific operation. Nothing more, nothing less, see definition of
> multiple dispatch. The specific way to double-dispatch that many Visitor
> examples (unfortunately, started with GoF book, and shown in Wikipedia)
> show, is simply the *implementation detail*. You do not have to trust me.
>
Do not touch multiple dispatch yet. Going there now is like going into
four dimensional math when you haven't yet got the planar 2D grid
to work. Stay firmly within studying double dispatch until you understand
what it is.

> Says Wikipedia:
>
> "In software engineering, double dispatch is a special form of multiple
> dispatch, and a mechanism that dispatches a function call to different
> concrete functions depending on the runtime types of two objects
> involved in the call"
>
> And, looking at same Wikipedia on multiple dispatch, C++ is *not* one of
> the languages that natively support multiple dispatch and there are
> multiple ways of implementing it, among them
> - "using run time type comparison via dynamic_cast" (if statement method)
> - "pointer-to-method lookup table"; and (misleadingly)
> - "the visitor pattern"
>
> Why "misleadingly"? Because, looking at visitor pattern, you find
>
> "The visitor takes the instance reference as input, and implements the
> goal through double dispatch."
>
> So, according to Wikipedia article on multiple dispatch one can
> implement it (among the others) using visitor pattern, whereas visitor
> pattern "implements the goal via double dispatch" whereas the double
> dispatch is a special form of multiple dispatch.
>
Yes. you were confused by first sentence in wikipedia and stopped to
read there. Double dispatch is multiple dispatch like 2D grid is special
case of 4 dimensional room with 2 dimensions being of zero size.

> Some readers (which seem to include you) resolve this circular reference
> ad-hoc: they see that *specific implementation* of multiple dispatch in
> C++ that happens to illustrate the Visitor article (stemming from the
> GoF book, unfortunately) and somehow decide that this is *the right way*
> (TM) to implement double dispatch. This is despite:
>
> - that double dispatch is defined as a concept an there is no *the right
> way* of doing it in C++ and Wikipedia itself illustrates two such ways
> (both different from the one used in the visitor article and GoF book)
> in the article on multiple dispatch.
>
> - that it must be obvious that it is Visitor that uses double dispatch
> in its implementation, not other may around, because the part cannot use
> the whole and Visitor is "bigger" than double-dispatch because it uses
> both double dispatch and iteration (through the objects in a data
> structure) but iteration is not a part of double dispatch.
>
> The problem with that (by and large, randomly-selected) implementation
> of double dispatch is that it is a bad choice to illustrate Visitor
> pattern specifically. Besides the mere distracting the reader to its
> clever *implementation details* (not essential for understanding the
> Visitor pattern), it also makes it difficult to emphasize the coherence
> and cohesiveness of the newly added operation and does nothing to
> explain how to organize the access of the new operation to all pieces of
> data it may need. Taking into account that the whole Visitor pattern is
> about adding a new operation to working on a collection or stream ("the
> structure") of objects, it is a very unfortunate choice indeed.
>
The whole point of calling o.accept(v) that then calls v.doOp(o) is double
dispatch. Otherwise why not to call v.doOp(o) right away and erase the
accept()? Think a bit, wall of text how everybody around are confused
does not make it true.
Free function does no way change hierarchy. Adding non-virtual
convenience method to base class of one of participants does
it minimally.

> This might be a valid choice sometimes (my first post started with
> this), but you pay by changing every class *every time you need a new
> operation* and making all Shape classes dependent on DrawingContext (and
> other contexts you might need for different operations (e.g. log file,
> network socket, database connection etc)). In visitor pattern, on the
> other hand, the concrete visitor is the only code that becomes dependent
> on that op-specific context.
>
It is totally OK choice for your presented use-case, visitor is unneeded
bloat there.

> >
> >>
> >> And this is the complete code for adding a new operation (virtual cast
> >> is to be only defined once for all operations)
> >
> > You forget that you have BorderingVisitor class of unclear life , inject
> > DrawingContext to it (is it copy?), etc. result is
> >
> > BorderingVisitor bv(dc);
> > s.acceptVisit(bv);
> >
> > instead one of:
> >
> > dc.draw_border(s);
> >
> > s.draw_border(dc);
> >
> > draw_border(dc, s);
> >
See? Crickets are chirping, no one is explaining why the bloat was even
needed.
Do not consider multiple dispatch before figuring what double
dispatch is.

> > These
> Unsure I understand what "these" refers to, will assume my doOp and
> acceptVisit functions
> > were entirely added to do double dispatch
>
> correct. acceptVisit dispatches by op (the 1st parameter of the outer
> structure "visit" operation) and doOp dispatches by object type (in my
> examples, either by using its base type's virtual function only or via a
> combination of the same and `if' statement on virtual cast result)
>
Except on your case it does not. There is just single function that
decides if to draw red or green border.

> > to add
> > virtual methods to large potentially unrelated data hierarchies without
> > changing any of classes in those hierarchies.
> I assume, by "potentially unrelated" data hierarchy, you mean the new op
> has to behave entirely differently on every type of the hierarchy and
> does not need common data. The issue with this assumption is that the
> very first op you add might relate to the classes like this with but the
> second might benefit from exploiting the type similarities or grouping
> them differently (not even necessary in groups compact in the hierarchy
> graph).
> > What you do is not even
> > use case for visitor pattern.
> Says Wikipedia:
>
> "
> What problems can the Visitor design pattern solve?
>
> It should be possible to define a new operation for (some) classes of an
> object structure without changing the classes.
> "

"When new operations are needed frequently and the object structure
consists of many unrelated classes, it's inflexible to add new subclasses
each time a new operation is required because "[..] distributing all these
operations across the various node classes leads to a system that's hard
to understand, maintain, and change."

Exactly. But if all you need is single little function then write single
little function. Neither single dispatch (virtual functions) nor double
dispatch (matching visitor classes to data hierarchy classes) are
needed for single little function. How did your function being in
visitor make anything easier to understand, maintain and change?

>
> "What I do" achieves the above IMO, so it is definitely the use case.
> You might argue whether or not my solution is a Visitor pattern (it is,
> at least it includes all canonical Visitor participants (Abstract
> Visitor, Concrete Visitors, Abstract Element, Concrete Elements (an
> object structure providing access to the elements is implied)) and none
> extra) but I don't understand how it can be argued that my code does not
> define a new operation for (some) classes of an object structure. And it
> surely does not change the classes.
>
That is especially bad and confusing when something looks like a
pattern, uses names from that pattern but actually isn't the pattern
at all in essence.

Pavel

unread,
Aug 24, 2023, 8:02:38 PM8/24/23
to
You threw an interesting tangential idea (that IoC-ness is a numerical
property of a pattern) that I am willing to explore but let's put the
main issue to bed first:

Are you saying that, for whatever cause (like making Visitor more
IoC-ish) you are willing to drastically narrow the class of problems
that the pattern commonly-known as Visitor is believed to solve?

Specifically, can there exist an IoC framework that you would be ready
to characterize as correctly implementing Visitor pattern if that
framework could visit elements of only one type?

Pavel

unread,
Aug 24, 2023, 11:12:09 PM8/24/23
to
Longer than what? Definitely the one function is shorter than the sum of
the length of all the element type hierarchy shims plus concrete visitor
visit overloads.

> bigger cyclomatic complexity,
with good code organization, no bigger than absolutely necessary.

> harder to understand, with good code organization, actually, easier (see my point below about
maintenance.

> harder to
> form confidence that it is valid,
see below in maintenance.

> harder to maintain,
actually, easier to maintain. For using that GoF / wiki DD
implementation, non-insignificant amount of scaffolding code has to be
written. Especially in a complex hierarchy, one can easily forget to
write that accept shim for a couple of classes where it is needed. E.g.
if in the hierarchy (or a part thereof) is "(B, C, D) > A" (> meaning
inheritance), imagine the op shall do same on A and D but different on B
and C. Isn't it easy to forget to add the accept shim to C (even though
the actual logic visit(C), at which the programmer is likely to focus,
is written all right).

> harder to test and
why?

> also may need more access to implementation details of all types
> involved, breaking encapsulation.
How so? All code is written in the op function that has no access to
internals.

>
>> In particular, as I explained
>> at the beginning, if the operation is decomposed correctly, its behavior
>> on the objects of different types has to be coherent, similar in some
>> way (and maybe more similar than different) and, as such, a significant
>> parts of the context or results of intermediate computations could be
>> shared.
>
> Translating data hierarchy to xml for particular version of particular
> protocol while supporting several such in parallel?
Imagine {ABCD) type hierarchy above is to be translated to XML and B and
C have to become XML elements with some non-trivially computable
attribute atr1 but XML elements produced from A and D don't need it.

> There can be
> intermediate computations shared but all are typically in in visitor
> classes or helpers of those. No one wants the classes in hierarchy
> to know anything about xml whatsoever,
correct but do not see how it is connected to anything I wrote

> nothing to talk about
> particular brand used in particular version of kinds of end-points.
let's not argue with anything I did not say, it's really irritating, I
will gladly give you an example, just explain why {ABCD} above is no good.

>
>> I demonstrated this in my example with red and green borders: a)
>> a condition by area prevents visitor from operating on objects is
>> computed from their property that is orthogonal to their dynamic type;
>> and b) the exact behavior of the operation (red vs green border in the
>> example) depends on a subset of types (which, in general, is not
>> necessarily compact in the hierarchy).
>>
> Your example did not need visitor pattern.
No example "needs" visitor pattern: any problem solved with visitor can
be solved in other ways, e.g. just by adding a virtual method to every
class. But *both* my examples could be perfectly solved with visitor. If
the one-time scaffolding is in place, I am ready to argue it is a good
solution.

>
>>> Think why virtual methods were added? To get rid of switch cases over type.
>> Of course, not. Virtual methods were *used*, with less or greater
>> success, to replace switch statements in event-processing scenarios
>> (like window behavior) (with greater success) or in state machines (with
>> lesser success).
>>
>> But, they were *invented* to implement polymorphic behavior and
>> relatively loose coupling (generally, lesser dependencies) between the
>> code that calls the method and the code that implements the method.
>>
> Same loose coupling is with code that calls function that does switch
> case over type, so that kind of victory wasn't achieved.
There is no such thing as "coupling in general". Any particular
dependency is some kind of coupling. If one has to add
accept(AbstractVisitor) method to the root of the type hierarchy, both
the type hierarchy's interface and implementation become compile-time
dependent on the new operation's interface (AbstractVisitor) (interface
may depend on forward-declaration only in C++, but not the
implementation so the implementation will also "see" N "visit" methods).
Additionally, because every AbstractVisitor depends on (the interface
of) every of (say, M) concrete element types, the Element's interface
implementation becomes dependent on every of M Element types (at least
their forward declarations, in C++). In this implementation, any new
operation adds the dependency, needs recompilation of class hierarchy etc.

If, as in my example, you only have one AbstractVisitor, adding 2nd and
further operations adds zero dependencies, don't require recompilation.

>
>> Obviously, a single switch statement couples everything together and the
>> compilation unit where the switch is depends on every piece of code and
>> every concrete type the switch statement handles -- to the extent its
>> behavior in the operation must differ from that of other types). But
>> this loose coupling is only desirable when the behaviors are severely
>> different.
>>
> I don't follow what you are saying. Different stock markets and other
> financial institutions for example are not severely different in essence,
> but the format of data you are expected to exchange with their servers
> differs massively enough.
Nah, it's the other way around. Say, BankA and BankB may have different
formats of their, say, client-side FIX protocol. But, they likely both
talk to NYSE using NYSE FIX/FAST and OATS reporting in exactly same or
almost same format.

Also, who needs to write an app that puts BankA and BankB to the same
hierarchy? It's unlinkely that it's one of BankA or BankB. More likely,
some regulatory agency or an exchange or settlement firm (hereinafter
*an outside firm*). For the purpose of the outside firm, the formats
it's interested in to operate on are likely the formats it uses to
communicate with those Banks (i.e. same or with small customizations). A
drastically different formats that BankA and B use to communicate with
their clients will unlikely be needed by an exchange (they could be
needed by a surveillance agency; but then "my" code would become
respectively simpler, *always staying simpler* than the correspondent
GoF code).

>
>> On the other hand, when the operation does *to significant
>> extent, a single thing* on different type, with 2-3 relatively small
>> type-dependent differences, these differences are often better expressed
>> in if statement, switch or in a number of different ways (including
>> calling combinations of virtual functions *from within the top-level
>> method implementing the operation*)
>>
> That is not use-case of visitor pattern.
You say it more than once and never cared to provide any reasoning. I
cited the cause to use Visitor (from Wikipedia you should like). That
cause is fully applicable for the above setup assuming one needs to add
many operations. I do not intend to repeat this again unless you provide
good reasoning.

>
>>> Visitor is not meant as excuse to add those back, otherwise just use
>>> the switch case in function, do not manufacture visitors that do not use
>>> double dispatch for confusion.
>>
>> Ohh. Not again. If you want to use virtual functions, use them, you
>> don't need visitor for this. Add virtual void newOperation(context
>> parameters) to the base class and call it in some for_each. This is
>> exactly what I explained in the first post: Visitor itself is an
>> alternative to adding a virtual methods.
>>
> You add same number of virtual methods of visitor if you need several
> different classes of visitors to visit over same data hierarchies. The
> gain is that these are in one place, in particular visitor and that the
> classes in hierarchies are not changed by single letter.
Yes, not changing classes when adding an op is the one of the causes for
using Visitor. Both my and Wiki visitor implementation formally achieve
this cause. Plain and simple. BUT, Wiki implementation changes the
compilation units of every Element (aka creates compilation dependency).
Plain and simple. My implementation doesn't (except for the very 1st
operation).
That's not I, that's you favorite ref source Wiki correctly says double
a dispatch is a special form of multiple dispatch and then, in all C++
examples it gives to show MD, it actually shows DD only. In fact, both
articles use same "Asteroid-Spaceship" example (DD) to exemplify either.

Now IMO, here is nothing magical in dispatching on dynamic types of 3
parameters as compared to the 2. It is surely more boring to express as
there is usually more functions to write; but otherwise, nothing too
exciting.

>
>> Says Wikipedia:
>>
>> "In software engineering, double dispatch is a special form of multiple
>> dispatch, and a mechanism that dispatches a function call to different
>> concrete functions depending on the runtime types of two objects
>> involved in the call"
>>
>> And, looking at same Wikipedia on multiple dispatch, C++ is *not* one of
>> the languages that natively support multiple dispatch and there are
>> multiple ways of implementing it, among them
>> - "using run time type comparison via dynamic_cast" (if statement method)
>> - "pointer-to-method lookup table"; and (misleadingly)
>> - "the visitor pattern"
>>
>> Why "misleadingly"? Because, looking at visitor pattern, you find
>>
>> "The visitor takes the instance reference as input, and implements the
>> goal through double dispatch."
>>
>> So, according to Wikipedia article on multiple dispatch one can
>> implement it (among the others) using visitor pattern, whereas visitor
>> pattern "implements the goal via double dispatch" whereas the double
>> dispatch is a special form of multiple dispatch.
>>
> Yes. you were confused by first sentence in wikipedia and stopped to
> read there. Double dispatch is multiple dispatch like 2D grid is special
> case of 4 dimensional room with 2 dimensions being of zero size.
Nothing like it. Implementing DD thru the manual scaffolding of what you
seem to believe *the one and the only correct implementation of DD for
Visitor pattern in C++" is boring and error-prone enough (see above
about maintenance) to not be the first choice in explaining Visitor.
You could, the complete class of Visitor problems is still solvable
without the accept shim. It is an implementation detail as well. It is
sometimes useful for the purposes other than that particular DD
implementation, e.g. to limit the exposure of the element types to the
"object structure" participant.

For an example of a framework implementing Visitor without shims, see
e.g. std::visit.

> Think a bit, wall of text how everybody around are confused
> does not make it true.
agree

>
>>>
>>>> The advantage is in having this logic in one place. If your visitor does
>>>> a coherent thing (which it is supposed to do; else why is it a single
>>>> visitor, there could be and usually would be common code for all or many
>>>> visited). The most pliant cases (that are exactly those gaining most
>>>> from applying a visitor pattern) may get by object virtual methods.
>>>> Simple example:
>>>>
>>>> If we wanted a green border around a shape of any of 10 classes derived
>>>> from convex polygon (ConvexTriangle, ConvexQuadrilateral etc), red
>>>> border around a shape of any of 10 classes derived from Oval and no
>>>> border around any of 30 other zero-area shape classes, all we need to do
>>>> is to change doOp to:
>>>>
>>>
>>> Unused cp in your code?
>> Yes, I started with taking bounding rectangle from every type separately
>> (which sometimes may be a valid micro-optimization if we use a fully
>> qualified non-virtual call; but then decided to keep it simple to
>> emphasize the points I wanted to emphasize)
>>> Can't post sane code from gg ... but also don't want to install newsreaders
>>> to all devices I use. It is drawing border to any shape and so it is unclear
>>> why you need visitor.
Because to decide whether to draw the border and (in the Example 2) its
color the element type is needed. I was about to introduce an
intermediate "ConvexArea" class but got lazy; regardless, Example 2
demonstrates the dependency on type.

Logical one place is either DrwawingContext's or
>>> Shape's nonvirtual method or free function:
>>>
>>> void Shape::draw_border(DrawingContext &dc) const {
>>> if (area() <= 25) return;
>>> const Rectangle br{boundingRectangle().increaseBy(0.1)};
>>> if (isConvexPolygon()) { dc.drawGreenRectangle(br); return; }
>>> assert(isOval());
>>> dc.drawRedRectangle(br);
>>> }
>> exactly, if you are ok to change the hierarchy every time you add an op,
>> you might not need the visitor, all you need is to call draw_border from
>> some for_each loop or high-order function.
>>
> Free function does no way change hierarchy. Adding non-virtual
> convenience method to base class of one of participants does
> it minimally.
See above about dependency and maintenance.

>
>> This might be a valid choice sometimes (my first post started with
>> this), but you pay by changing every class *every time you need a new
>> operation* and making all Shape classes dependent on DrawingContext (and
>> other contexts you might need for different operations (e.g. log file,
>> network socket, database connection etc)). In visitor pattern, on the
>> other hand, the concrete visitor is the only code that becomes dependent
>> on that op-specific context.
>>
> It is totally OK choice for your presented use-case, visitor is unneeded
> bloat there.
It *is* a Visitor as it has all the participants and solve the problem.

>
>>>
>>>>
>>>> And this is the complete code for adding a new operation (virtual cast
>>>> is to be only defined once for all operations)
>>>
>>> You forget that you have BorderingVisitor class of unclear life , inject
>>> DrawingContext to it (is it copy?), etc. result is
>>>
>>> BorderingVisitor bv(dc);
>>> s.acceptVisit(bv);
>>>
>>> instead one of:
>>>
>>> dc.draw_border(s);
dc is not supposed to know anything about a Shape, It draws a
rectangular body given a Rectangle only to specify the exact geometry of
the Shape. It is not a business of dc to compute. What if I want to,
instead of as a border of the size of the shape-cirumscribing rectangle
increased by 0.1 unit in all directions, add margins of 2 units at the
right and 1 unit at other 3 directions? That increaseBy call in my
example code is there for reason.

>>>
>>> s.draw_border(dc);
>>>
>>> draw_border(dc, s);
>>>
> See? Crickets are chirping,
no, they are strangely croaking, the border does not look like I wanted
it to, see above.

> no one is explaining why the bloat was even
> needed.
someone did but someone else did not like to read. Where's my increaseBy?
not again. I already cited the definition of what DD is. You refer to a
particular implementation as ff it were the definition.

>
>>> These
>> Unsure I understand what "these" refers to, will assume my doOp and
>> acceptVisit functions
>>> were entirely added to do double dispatch
>>
>> correct. acceptVisit dispatches by op (the 1st parameter of the outer
>> structure "visit" operation) and doOp dispatches by object type (in my
>> examples, either by using its base type's virtual function only or via a
>> combination of the same and `if' statement on virtual cast result)
>>
> Except on your case it does not. There is just single function that
> decides if to draw red or green border.
yes, depending on the type. Do you expect colors, styles, font foundries
and textures from an example on visitor? You are welcome to provide your
own code (the above does not work).

>
>>> to add
>>> virtual methods to large potentially unrelated data hierarchies without
>>> changing any of classes in those hierarchies.
>> I assume, by "potentially unrelated" data hierarchy, you mean the new op
>> has to behave entirely differently on every type of the hierarchy and
>> does not need common data. The issue with this assumption is that the
>> very first op you add might relate to the classes like this with but the
>> second might benefit from exploiting the type similarities or grouping
>> them differently (not even necessary in groups compact in the hierarchy
>> graph).
>>> What you do is not even
>>> use case for visitor pattern.
>> Says Wikipedia:
>>
>> "
>> What problems can the Visitor design pattern solve?
>>
>> It should be possible to define a new operation for (some) classes of an
>> object structure without changing the classes.
>> "
>
> "When new operations are needed frequently and the object structure
> consists of many unrelated classes, it's inflexible to add new subclasses
> each time a new operation is required because "[..] distributing all these
> operations across the various node classes leads to a system that's hard
> to understand, maintain, and change."
You know what you did? You made me dig out my (licensed) CD-ROM with GoF
book. Thank you! As I suspected, the Wikipedia quoted GoF in absolutely
wrong context. There is nothing about "adding new subclasses" in GoF
book, it is all about changing Node hierachy classes. The complete
paragraph says:

"
This diagram shows part of the Node class hierarchy. The problem here is
that distributing all these operations across the various node classes
leads to a system that's hard to understand, maintain, and change. It
will be confusing to have type-checking code mixed with pretty-printing
code or flow analysis code. Moreover, adding a new operation usually
requires recompiling all of these classes. It would be better if each
new operation could be added separately, and the node classes were
independent of the operations that apply to them.
"
(this paragraph is below the diagram depicting a part of Element
hierarchy, has nothing to do to Visitor classes or adding "new
subclasses". Unsure where this came from).

Whoever wrote this page should be .. well, politely reprimanded.

The quote above also emphasizes my point that adding compilation
dependency to the element type hierarchy shall not be taken lightly.


>
> Exactly. But if all you need is single little function then write single
> little function. Neither single dispatch (virtual functions) nor double
> dispatch (matching visitor classes to data hierarchy classes) are
> needed for single little function. How did your function being in
> visitor make anything easier to understand, maintain and change?
How many operations should I have exemplified? I showed 2 and stated the
Visitor is applicable when one is to add plenty -- what else do you
expect, not providing a single complete counterexample for the same problem.

>
>>
>> "What I do" achieves the above IMO, so it is definitely the use case.
>> You might argue whether or not my solution is a Visitor pattern (it is,
>> at least it includes all canonical Visitor participants (Abstract
>> Visitor, Concrete Visitors, Abstract Element, Concrete Elements (an
>> object structure providing access to the elements is implied)) and none
>> extra) but I don't understand how it can be argued that my code does not
>> define a new operation for (some) classes of an object structure. And it
>> surely does not change the classes.
>>
> That is especially bad and confusing when something looks like a
> pattern, uses names from that pattern but actually isn't the pattern
> at all in essence.

Why is it you and not I who gets to define the "essence"? I stated the
formal Visitor applicability conditions exactly to make this discussion
objective, to the point, and maybe agree to something useful. How can we
agree on anything useful if I start referring to some esoteric "essence"
as I understand it?




Bonita Montero

unread,
Aug 24, 2023, 11:13:58 PM8/24/23
to
Am 25.08.2023 um 02:02 schrieb Pavel:

> Are you saying that, for whatever cause (like making Visitor more
> IoC-ish) you are willing to drastically narrow the class of problems
> that the pattern commonly-known as Visitor is believed to solve?

I suggested a way to make the vistitor pattern implementable with much
less code and more maintainable. The visitor neither need an addtional
base-class and implementation class.

Michael S

unread,
Aug 25, 2023, 8:21:43 AM8/25/23
to
After trying, with limited success, to follow the discussion between Öö Tiib and Pavel,
I am coming to conclusion that continued ignorance about Visitor Pattern is best both
for my blood pressure and for my programming skills.

Bonita Montero

unread,
Aug 25, 2023, 8:33:34 AM8/25/23
to
Am 25.08.2023 um 14:21 schrieb Michael S:

> After trying, with limited success, to follow the discussion between Öö Tiib and Pavel,
> I am coming to conclusion that continued ignorance about Visitor Pattern is best both
> for my blood pressure and for my programming skills.

I'm not ignore it but I wanted to say that it's easier to implement
it not with a separate base and dervied class for the visitor but
with a function<>-object. That would save a lot of work and you get
the same performance.

Öö Tiib

unread,
Aug 25, 2023, 10:52:51 AM8/25/23
to
Can't. You can not leak knowledge of particular operation implemented
by VisitorX that happens to be same for A and D into base visitor. Base
visitor has to have all. Something like:
virtual doOp(A& a) {std::abort();}
virtual doOp(B& b) {std::abort();}
virtual doOp(C& c) {std::abort();}
virtual doOp(D& d) {std::abort();}

It is because different operation implemented by VisitorY may be
same for A and B and different for C and D. You can only leave out
handling of D in derived visitor if D is never processed by that visitor,
so std::abort() suits you.

> > harder to test and
> why?
> > also may need more access to implementation details of all types
> > involved, breaking encapsulation.
> How so? All code is written in the op function that has no access to
> internals.
>
You have example where you need short function and then argue against
whole existence of issues why procedural programming was invented in
fifties, structured programming was invented in sixties, object-oriented
programming and double dispatch were invented in seventies. The issues
are still real in real projects, and ignoring these is called code smell.
I can not be expected to teach all that knowledge of decades in one post.
Lets take step by step?

> >
> >> In particular, as I explained
> >> at the beginning, if the operation is decomposed correctly, its behavior
> >> on the objects of different types has to be coherent, similar in some
> >> way (and maybe more similar than different) and, as such, a significant
> >> parts of the context or results of intermediate computations could be
> >> shared.
> >
> > Translating data hierarchy to xml for particular version of particular
> > protocol while supporting several such in parallel?
>
> Imagine {ABCD) type hierarchy above is to be translated to XML and B and
> C have to become XML elements with some non-trivially computable
> attribute atr1 but XML elements produced from A and D don't need it.
>
Because different endpoints (for what you form the XML) want different
data, one wants like you describe ... other does not want atr1 for A and B
... while third does not want information about D to be ever sent to it.
Now you have 3 different visitors making XML for each endpoint and any
logic in base visitor about first hurts other two.

> > There can be
> > intermediate computations shared but all are typically in in visitor
> > classes or helpers of those. No one wants the classes in hierarchy
> > to know anything about xml whatsoever,
> correct but do not see how it is connected to anything I wrote
> > nothing to talk about
> > particular brand used in particular version of kinds of end-points.
> let's not argue with anything I did not say, it's really irritating, I
> will gladly give you an example, just explain why {ABCD} above is no good.
>
I tried to.

> >
> >> I demonstrated this in my example with red and green borders: a)
> >> a condition by area prevents visitor from operating on objects is
> >> computed from their property that is orthogonal to their dynamic type;
> >> and b) the exact behavior of the operation (red vs green border in the
> >> example) depends on a subset of types (which, in general, is not
> >> necessarily compact in the hierarchy).
> >>
> > Your example did not need visitor pattern.
>
> No example "needs" visitor pattern: any problem solved with visitor can
> be solved in other ways, e.g. just by adding a virtual method to every
> class. But *both* my examples could be perfectly solved with visitor. If
> the one-time scaffolding is in place, I am ready to argue it is a good
> solution.
>
True about adding virtual method, just that adding 3 virtual functions for XML
generation code into every class will clutter code of those classes.
Especially if you want (some of) same classes to be used in other project
that does not communicate with said end-points at all.

> >
> >>> Think why virtual methods were added? To get rid of switch cases over type.
> >> Of course, not. Virtual methods were *used*, with less or greater
> >> success, to replace switch statements in event-processing scenarios
> >> (like window behavior) (with greater success) or in state machines (with
> >> lesser success).
> >>
> >> But, they were *invented* to implement polymorphic behavior and
> >> relatively loose coupling (generally, lesser dependencies) between the
> >> code that calls the method and the code that implements the method.
> >>
> > Same loose coupling is with code that calls function that does switch
> > case over type, so that kind of victory wasn't achieved.
>
> There is no such thing as "coupling in general".

Where I said the quoted text "coupling in general"? What you mean by it?
And why you switch to visitors when discussing benefits of virtual
methods?

> Any particular
> dependency is some kind of coupling. If one has to add
> accept(AbstractVisitor) method to the root of the type hierarchy, both
> the type hierarchy's interface and implementation become compile-time
> dependent on the new operation's interface (AbstractVisitor) (interface
> may depend on forward-declaration only in C++, but not the
> implementation so the implementation will also "see" N "visit" methods).
> Additionally, because every AbstractVisitor depends on (the interface
> of) every of (say, M) concrete element types, the Element's interface
> implementation becomes dependent on every of M Element types (at least
> their forward declarations, in C++). In this implementation, any new
> operation adds the dependency, needs recompilation of class hierarchy etc.
>
The whole point of visitor pattern is that you need to add accept of base
visitor only once to every class visited, no need to add 3 virtual methods to
generate XML for 3 different endpoints. But can't simplify from there,
otherwise the pattern is broken.

> If, as in my example, you only have one AbstractVisitor, adding 2nd and
> further operations adds zero dependencies, don't require recompilation.
>
No class needs to be recompiled when 4th server needing obscure
abomination of JSON is added. Same stock

void WhateverClass::accept(BaseVisitor& s) override { s.doOp(*this); }

will work like these already did. Only

class ObscureJSONGeneratorVisitor: public BaseVisitor { /* ... */ };

will need to be implemented.

> >
> >> Obviously, a single switch statement couples everything together and the
> >> compilation unit where the switch is depends on every piece of code and
> >> every concrete type the switch statement handles -- to the extent its
> >> behavior in the operation must differ from that of other types). But
> >> this loose coupling is only desirable when the behaviors are severely
> >> different.
> >>
> > I don't follow what you are saying. Different stock markets and other
> > financial institutions for example are not severely different in essence,
> > but the format of data you are expected to exchange with their servers
> > differs massively enough.
>
> Nah, it's the other way around. Say, BankA and BankB may have different
> formats of their, say, client-side FIX protocol. But, they likely both
> talk to NYSE using NYSE FIX/FAST and OATS reporting in exactly same or
> almost same format.
>
They talk with NYSE in manner that NYSE requires, but that does not concern
you, you are not NYSE that financial institutions themselves have to care how
to be conformant with. You are probably sucking the client-side butt holes of
theirs.

> Also, who needs to write an app that puts BankA and BankB to the same
> hierarchy? It's unlinkely that it's one of BankA or BankB. More likely,
> some regulatory agency or an exchange or settlement firm (hereinafter
> *an outside firm*). For the purpose of the outside firm, the formats
> it's interested in to operate on are likely the formats it uses to
> communicate with those Banks (i.e. same or with small customizations). A
> drastically different formats that BankA and B use to communicate with
> their clients will unlikely be needed by an exchange (they could be
> needed by a surveillance agency; but then "my" code would become
> respectively simpler, *always staying simpler* than the correspondent
> GoF code).
>
So you never heard of anyone who happens to have accounts in multiple
banks (even in multiple countries)? I suggest urgently to read up on
Silicon Valley Bank, Signature Bank, First Republic Bank and Heartland
Tri-State Bank ... your bank may be next.

> >
> >> On the other hand, when the operation does *to significant
> >> extent, a single thing* on different type, with 2-3 relatively small
> >> type-dependent differences, these differences are often better expressed
> >> in if statement, switch or in a number of different ways (including
> >> calling combinations of virtual functions *from within the top-level
> >> method implementing the operation*)
> >>
> > That is not use-case of visitor pattern.
> You say it more than once and never cared to provide any reasoning. I
> cited the cause to use Visitor (from Wikipedia you should like). That
> cause is fully applicable for the above setup assuming one needs to add
> many operations. I do not intend to repeat this again unless you provide
> good reasoning.
>
I did, you just do not read.
Wikipedia is correct, but multiple dispatch is way over head when you can't
apparently figure what is double dispatch ... so go not into multiple.

> Now IMO, here is nothing magical in dispatching on dynamic types of 3
> parameters as compared to the 2. It is surely more boring to express as
> there is usually more functions to write; but otherwise, nothing too
> exciting.
>
You can't understand rectangle yet so parallelepiped is way too early.
Then you are out of luck as you talk about something that is not visitor
pattern.
"In essence, the visitor allows adding new virtual functions to a family of
classes, without modifying the classes. Instead, a visitor class is created
that implements **all** of the appropriate specializations of the virtual
function. The visitor takes the instance reference as input, and implements
the goal through **double dispatch**."

You argue with that and GoF not me.
IOW as you do something else but visitor the accept() is unneeded
implementation detail, but for whatever cargo cult reason you added
that accept().

> For an example of a framework implementing Visitor without shims, see
> e.g. std::visit.
>
There you go into another paradigm entirely, generic programming and
specific tagged union implementation. Note if constexpr's used.
So the dispatch is done compile time by generic lambda. Generic
programming was invented also in seventies. C++ is multi-paradigm
programming language. Don't confuse unrelated paradigms into
visitor that is pattern of object-oriented programming and uses double
dispatch.
I repeat:

void Shape::draw_border(DrawingContext &dc) const {
if (area() <= 25) return;
const Rectangle br{boundingRectangle().increaseBy(0.1)};

**Try reading above line extra carefully.**

if (isConvexPolygon()) { dc.drawGreenRectangle(br); return; }
assert(isOval());
dc.drawRedRectangle(br);
}

Can easily transform into free function if Shape is bad place.
It is multiple dispatch, but do not go there as multiple dispatch you can't
grasp yet.

> >
> >>> These
> >> Unsure I understand what "these" refers to, will assume my doOp and
> >> acceptVisit functions
> >>> were entirely added to do double dispatch
> >>
> >> correct. acceptVisit dispatches by op (the 1st parameter of the outer
> >> structure "visit" operation) and doOp dispatches by object type (in my
> >> examples, either by using its base type's virtual function only or via a
> >> combination of the same and `if' statement on virtual cast result)
> >>
> > Except on your case it does not. There is just single function that
> > decides if to draw red or green border.
> yes, depending on the type. Do you expect colors, styles, font foundries
> and textures from an example on visitor? You are welcome to provide your
> own code (the above does not work).
>
It was doing exactly same as your posted code.
The visitor pattern examples are present in GoF book and in Wikipedia
article. You argue with those, what is the point of me adding one more?

> >
> >>
> >> "What I do" achieves the above IMO, so it is definitely the use case.
> >> You might argue whether or not my solution is a Visitor pattern (it is,
> >> at least it includes all canonical Visitor participants (Abstract
> >> Visitor, Concrete Visitors, Abstract Element, Concrete Elements (an
> >> object structure providing access to the elements is implied)) and none
> >> extra) but I don't understand how it can be argued that my code does not
> >> define a new operation for (some) classes of an object structure. And it
> >> surely does not change the classes.
> >>
> > That is especially bad and confusing when something looks like a
> > pattern, uses names from that pattern but actually isn't the pattern
> > at all in essence.
> Why is it you and not I who gets to define the "essence"? I stated the
> formal Visitor applicability conditions exactly to make this discussion
> objective, to the point, and maybe agree to something useful. How can we
> agree on anything useful if I start referring to some esoteric "essence"
> as I understand it?
>
The OOP pattern we discuss is not invented by me or you. I don't change
it into "less boring" or whatever. It is what it is. You do change it. So you do
not argue with my "personal definitions" here.

Öö Tiib

unread,
Aug 25, 2023, 11:28:31 AM8/25/23
to
Brilliant decision. Over the decades I have couple times seen visitor misused in
manner that turned the part of code base very hard to maintain. Team members
changed 2 story-point estimation into 20 each time someone realised "but hey,
then we need to change logic in ####Visitor." It had defective dispatch and then
it needed maintenance that made it to grow into kind of cancer no one dared to
touch.

Pavel

unread,
Aug 26, 2023, 12:56:07 AM8/26/23
to
??? You forgot to add accept function to C. Then your object of dynamic
type C will be happily dispatched to VisitorX::visitElementA by A::accept.

You can not leak knowledge of particular operation implemented
> by VisitorX that happens to be same for A and D into base visitor. Base
> visitor has to have all. Something like:
> virtual doOp(A& a) {std::abort();}
> virtual doOp(B& b) {std::abort();}
> virtual doOp(C& c) {std::abort();}
> virtual doOp(D& d) {std::abort();}
>
> It is because different operation implemented by VisitorY may be
> same for A and B and different for C and D. You can only leave out
> handling of D in derived visitor if D is never processed by that visitor,
> so std::abort() suits you.
what do argue against?

>
>>> harder to test and
>> why?
>>> also may need more access to implementation details of all types
>>> involved, breaking encapsulation.
>> How so? All code is written in the op function that has no access to
>> internals.
>>
> You have example where you need short function and then argue against
> whole existence of issues why procedural programming was invented in
> fifties, structured programming was invented in sixties, object-oriented
> programming and double dispatch were invented in seventies. The issues
> are still real in real projects, and ignoring these is called code smell.
> I can not be expected to teach all that knowledge of decades in one post.
> Lets take step by step?
You are still to provide an example. For now, there nothing to take
apart step by step except for my code that you are not even trying to
take apart.

>
>>>
>>>> In particular, as I explained
>>>> at the beginning, if the operation is decomposed correctly, its behavior
>>>> on the objects of different types has to be coherent, similar in some
>>>> way (and maybe more similar than different) and, as such, a significant
>>>> parts of the context or results of intermediate computations could be
>>>> shared.
>>>
>>> Translating data hierarchy to xml for particular version of particular
>>> protocol while supporting several such in parallel?
>>
>> Imagine {ABCD) type hierarchy above is to be translated to XML and B and
>> C have to become XML elements with some non-trivially computable
>> attribute atr1 but XML elements produced from A and D don't need it.
>>
> Because different endpoints (for what you form the XML) want different
> data, one wants like you describe ... other does not want atr1 for A and B
> ... while third does not want information about D to be ever sent to it.
> Now you have 3 different visitors making XML for each endpoint and any
> logic in base visitor about first hurts other two.
What does your "Because" refer to? There is no "why" in my text (and no
question mark at all). I thought you disliked walls of words? What are
"endpoints"? I do not understand what "make XML for an endpoint" means.
I likely format XML to whatever output stream the visitor caller
provides. If that stream is an endpoint, what significance it has to
this discussion?

>
>>> There can be
>>> intermediate computations shared but all are typically in in visitor
>>> classes or helpers of those. No one wants the classes in hierarchy
>>> to know anything about xml whatsoever,
>> correct but do not see how it is connected to anything I wrote
>>> nothing to talk about
>>> particular brand used in particular version of kinds of end-points.
>> let's not argue with anything I did not say, it's really irritating, I
>> will gladly give you an example, just explain why {ABCD} above is no good.
>>
> I tried to.
No, your words above do not refer to my argument even grammatically.

>
>>>
>>>> I demonstrated this in my example with red and green borders: a)
>>>> a condition by area prevents visitor from operating on objects is
>>>> computed from their property that is orthogonal to their dynamic type;
>>>> and b) the exact behavior of the operation (red vs green border in the
>>>> example) depends on a subset of types (which, in general, is not
>>>> necessarily compact in the hierarchy).
>>>>
>>> Your example did not need visitor pattern.
>>
>> No example "needs" visitor pattern: any problem solved with visitor can
>> be solved in other ways, e.g. just by adding a virtual method to every
>> class. But *both* my examples could be perfectly solved with visitor. If
>> the one-time scaffolding is in place, I am ready to argue it is a good
>> solution.
>>
> True about adding virtual method, just that adding 3 virtual functions for XML
> generation code into every class will clutter code of those classes.
You are essentially repeating arguments for Visitor from
advantage/drawback statements on my first post. What purpose is this
repetition supposed to serve in this discussion?

> Especially if you want (some of) same classes to be used in other project
> that does not communicate with said end-points at all.
what is "end-points"? I am addressing every your point but your
"responses" don't even refer to mine. Can you keep to the point and not
go on tangents please?

I will make last honest effort to structure this discussion to something
legible: do you agree that we disagree on the following points:

1. That my examples demonstrate adding an op to a correctly built
Visitor pattern, assuming verbally described scaffolding (I say yes, you
say no)

2. That any correct implementation of a Visitor pattern in C++ shall use
that specific implementation of DD that GoF and Wikipedia happen to use.
(I say no, you say yes)

?

It is the implication of my opinions on #1 and #2 above that Visitor can
be tought on smaller and more to-the-point examples. But we don't need
to argue about this until we put to bed #1 and #2. Agree?

>
>>>
>>>>> Think why virtual methods were added? To get rid of switch cases over type.
>>>> Of course, not. Virtual methods were *used*, with less or greater
>>>> success, to replace switch statements in event-processing scenarios
>>>> (like window behavior) (with greater success) or in state machines (with
>>>> lesser success).
>>>>
>>>> But, they were *invented* to implement polymorphic behavior and
>>>> relatively loose coupling (generally, lesser dependencies) between the
>>>> code that calls the method and the code that implements the method.
>>>>
>>> Same loose coupling is with code that calls function that does switch
>>> case over type, so that kind of victory wasn't achieved.
>>
>> There is no such thing as "coupling in general".
>
> Where I said the quoted text "coupling in general"? What you mean by it?
You mentioned coupling without specifying what particular coupling you
meant in what particular code so it is impossible for me to guess what
you mean. Therefore I reasonably called your reference to coupling as
reference to "coupling in general".

> And why you switch to visitors when discussing benefits of virtual
> methods?
It was you who went on tangent about virtual functions asking me to
think why "virtual methods were added" and then provided incorrect
answer to your own question. I simply corrected that incorrect answer of
yours.

>
>> Any particular
>> dependency is some kind of coupling. If one has to add
>> accept(AbstractVisitor) method to the root of the type hierarchy, both
>> the type hierarchy's interface and implementation become compile-time
>> dependent on the new operation's interface (AbstractVisitor) (interface
>> may depend on forward-declaration only in C++, but not the
>> implementation so the implementation will also "see" N "visit" methods).
>> Additionally, because every AbstractVisitor depends on (the interface
>> of) every of (say, M) concrete element types, the Element's interface
>> implementation becomes dependent on every of M Element types (at least
>> their forward declarations, in C++). In this implementation, any new
>> operation adds the dependency, needs recompilation of class hierarchy etc.
>>
> The whole point of visitor pattern is that you need to add accept of base
> visitor only once to every class visited, no need to add 3 virtual methods to
> generate XML for 3 different endpoints. But can't simplify from there,
> otherwise the pattern is broken.
Another bareword. How exactly this breaks the pattern? Does operation
start to behave not as specified? -- IMO, no. Do we introduce extra
participant to the pattern to "save" the desired behavior by adding
extra entity? -- IMO, no. Do we force a compilation unit depend on more
entities? -- IMO, no. So, what exactly do we break?

>
>> If, as in my example, you only have one AbstractVisitor, adding 2nd and
>> further operations adds zero dependencies, don't require recompilation.
>>
> No class needs to be recompiled when 4th server needing obscure
> abomination of JSON is added.
When did I say the opposite? I just counted the number of dependencies
(that have to be there from the outset). Every type in the hierarchy
becomes dependent on any other type because its accept method depends on
AbstractVisitor and it has methods dependent on every type in the
hierarchy, at least its forward declaration. This may be also true for
my approach if specific type implementation is dispatched with virtual
cast function (because we need to define them at the base class) or not
if we use typeid or enum or pattern matching.

> Same stock
>
> void WhateverClass::accept(BaseVisitor& s) override { s.doOp(*this); }
>
> will work like these already did. Only
>
> class ObscureJSONGeneratorVisitor: public BaseVisitor { /* ... */ };
>
> will need to be implemented.
>
>>>
>>>> Obviously, a single switch statement couples everything together and the
>>>> compilation unit where the switch is depends on every piece of code and
>>>> every concrete type the switch statement handles -- to the extent its
>>>> behavior in the operation must differ from that of other types). But
>>>> this loose coupling is only desirable when the behaviors are severely
>>>> different.
>>>>
>>> I don't follow what you are saying. Different stock markets and other
>>> financial institutions for example are not severely different in essence,
>>> but the format of data you are expected to exchange with their servers
>>> differs massively enough.
>>
>> Nah, it's the other way around. Say, BankA and BankB may have different
>> formats of their, say, client-side FIX protocol. But, they likely both
>> talk to NYSE using NYSE FIX/FAST and OATS reporting in exactly same or
>> almost same format.
>>
> They talk with NYSE in manner that NYSE requires, but that does not concern
> you, you are not NYSE that financial institutions themselves have to care how
> to be conformant with. You are probably sucking the client-side butt holes of
> theirs.
See below why you are likely NYSE if you care about supporting different
financial institutions.
>
>> Also, who needs to write an app that puts BankA and BankB to the same
>> hierarchy? It's unlinkely that it's one of BankA or BankB. More likely,
>> some regulatory agency or an exchange or settlement firm (hereinafter
>> *an outside firm*). For the purpose of the outside firm, the formats
>> it's interested in to operate on are likely the formats it uses to
>> communicate with those Banks (i.e. same or with small customizations). A
>> drastically different formats that BankA and B use to communicate with
>> their clients will unlikely be needed by an exchange (they could be
>> needed by a surveillance agency; but then "my" code would become
>> respectively simpler, *always staying simpler* than the correspondent
>> GoF code).
>>
> So you never heard of anyone who happens to have accounts in multiple
> banks (even in multiple countries)? I suggest urgently to read up on
> Silicon Valley Bank, Signature Bank, First Republic Bank and Heartland
> Tri-State Bank ... your bank may be next.
Is your purpose to throw as many tangents as needed to get me confused?
If I have account in a couple of banks, am I suppose to write a program
to do my banking?

>
>>>
>>>> On the other hand, when the operation does *to significant
>>>> extent, a single thing* on different type, with 2-3 relatively small
>>>> type-dependent differences, these differences are often better expressed
>>>> in if statement, switch or in a number of different ways (including
>>>> calling combinations of virtual functions *from within the top-level
>>>> method implementing the operation*)
>>>>
>>> That is not use-case of visitor pattern.
Bareword again. Nowhere in Visitor pattern rationale and motivation does
it say that it should only be used when the behavior of an op on
different hierarchy types should not have commonalities. All it says
that operations are unrelated one to another.

One actually has to expect there will be commonalities on applying an op
to objects of different types of the hierarchy; else why are these types
are in this same hierarchy?

>> You say it more than once and never cared to provide any reasoning. I
>> cited the cause to use Visitor (from Wikipedia you should like). That
>> cause is fully applicable for the above setup assuming one needs to add
>> many operations. I do not intend to repeat this again unless you provide
>> good reasoning.
>>
> I did, you just do not read.
I read everything your wrote.
Who does not read here? Trying once again: WIKIPEDIA C++ EXAMPLES ON MD
ACTUALLY DEMONSTRATE DD. How can a simple example of MD that is DD be
over head if you insist that another example of DD (actually,
implemented in a messier manner) has to be understood first?

>
>> Now IMO, here is nothing magical in dispatching on dynamic types of 3
>> parameters as compared to the 2. It is surely more boring to express as
>> there is usually more functions to write; but otherwise, nothing too
>> exciting.
>>
> You can't understand rectangle yet so parallelepiped is way too early.
Now your are convincing.
hah?
> as you talk about something that is not visitor
> pattern.
I do not talk about a particular implementation of it that you are so
fond of, correct.

> "In essence, the visitor allows adding new virtual functions to a family of
> classes, without modifying the classes. Instead, a visitor class is created
> that implements **all** of the appropriate specializations of the virtual
> function. The visitor takes the instance reference as input, and implements
> the goal through **double dispatch**."
>
> You argue with that and GoF not me.
where? You should have emphasized the word "appropriate" rather than
"all". Double dispatch is correctly emphasized, but your fondness to one
particular kind of it does not mean there is no others.
They are only unrelated in your mind. Design patterns are not libraries,
they can be implemented in different ways and in different paradigms.
You do repeat.. my code. Where is your beloved DD now? All of a sudden
you are checking the shape type with isConfexPolygon(). Makes sense, you
are beginning to get it. Although it is unclear why assert(isOval()); is
not supposed to fire so you have some work to do.
very convincing. hominem unius libri timeo

>
>>>
>>>>> These
>>>> Unsure I understand what "these" refers to, will assume my doOp and
>>>> acceptVisit functions
>>>>> were entirely added to do double dispatch
>>>>
>>>> correct. acceptVisit dispatches by op (the 1st parameter of the outer
>>>> structure "visit" operation) and doOp dispatches by object type (in my
>>>> examples, either by using its base type's virtual function only or via a
>>>> combination of the same and `if' statement on virtual cast result)
>>>>
>>> Except on your case it does not. There is just single function that
>>> decides if to draw red or green border.
>> yes, depending on the type. Do you expect colors, styles, font foundries
>> and textures from an example on visitor? You are welcome to provide your
>> own code (the above does not work).
>>
> It was doing exactly same as your posted code.
What is that "it"? One more nonsense and plonk your posts.
[plonked]

Pavel

unread,
Aug 26, 2023, 1:05:20 AM8/26/23
to
Visitor does not need anything, it simply solves a particular class of
problems that happens to include a structure of objects of multiple
types. Your visitor implementation is invalid if it cannot solve a valid
visitor problem.

Pavel

unread,
Aug 26, 2023, 1:17:54 AM8/26/23
to
You are right, no good deed goes unpunished. I should have known better.

-Pavel

Bonita Montero

unread,
Aug 26, 2023, 3:24:51 AM8/26/23
to
Am 26.08.2023 um 07:05 schrieb Pavel:

> Visitor does not need anything, it simply solves a particular class
> of problems that happens to include a structure of objects of multiple
> types. Your visitor implementation is invalid if it cannot solve a valid
> visitor problem.

Either I didn't describe the difference to what you imagine correctly
or you just didn't understand me. In the Visitor pattern, the Visitor
needs a base class with a virtual method to derive from, and the derived
object is passed by reference to the Visitor function of the objects
being visited. However, the derived and the base class are superfluous
with functional programming. Since C++11 you can simply request a
function<> object and conveniently you just pass a lambda that when
called, a temporary function<> object is created that represents the
interface to the object that can be visited.

Mut...@dastardlyhq.com

unread,
Aug 26, 2023, 4:01:51 AM8/26/23
to
This sort of programming is the equivalent of touchscreens in cars - looks
great and impresses the "Ooo shiny!" crowd but adds little to the main
functionality of the car and just adds needless complexity with more scope for
failure.

Bonita Montero

unread,
Aug 26, 2023, 5:29:50 AM8/26/23
to
Am 26.08.2023 um 10:01 schrieb Mut...@dastardlyhq.com:

> This sort of programming is the equivalent of touchscreens in cars - looks
> great and impresses the "Ooo shiny!" crowd but adds little to the main
> functionality of the car and just adds needless complexity with more scope
> for failure.

My solution is less complexity because you don't need to declare
a base class and a derived class. Just declare the parameter as
a function object and pass a lambda which is automatically crated.
That's for sure at maximally the tenth of the code.
You can have functional programming in C++ since 2011. That's a
long time you didn't notice its potential.

Mut...@dastardlyhq.com

unread,
Aug 26, 2023, 11:01:03 AM8/26/23
to
People who say you can have functional programming in C++ don't understand
functional programming. Its more than just no cost recursion , its also
polymorphism by *VALUE*. That is not and probably never will be possible in
C++.

Bonita Montero

unread,
Aug 26, 2023, 11:08:28 AM8/26/23
to
Am 26.08.2023 um 17:00 schrieb Mut...@dastardlyhq.com:

> People who say you can have functional programming in C++ don't understand
> functional programming. Its more than just no cost recursion , its also
> polymorphism by *VALUE*. ...

That's what I described and you didn't notice it. A function<>-object
is always polymorphic. I bet you never used function<>-objects in C++.


Mut...@dastardlyhq.com

unread,
Aug 26, 2023, 11:15:44 AM8/26/23
to
Given they're necessary for the STL what do you think?

Which part of "by value" is confusing you? But if you insist on claiming its
possible show us how you'd do polymorphism for a function with a single int
param for say the values 0, 2, 3, 10 -> 100 and a default.

Bonita Montero

unread,
Aug 26, 2023, 11:24:14 AM8/26/23
to
You can't describe what you want and you don't understand function<>
-objects. Stick with C.

Bonita Montero

unread,
Aug 26, 2023, 11:27:12 AM8/26/23
to
Am 26.08.2023 um 17:15 schrieb Mut...@dastardlyhq.com:
Somehow you are also quite disturbed. I wanted to tell nru that the
visitor pattern in C++ can be greatly simplified with a c++11 function<>
object and you are offended by this mere statement that you have to
explain the possibilities of C++11 as insufficient at this point to
put you above me. The therapist that matches you has yet to be born.

Mut...@dastardlyhq.com

unread,
Aug 26, 2023, 11:36:53 AM8/26/23
to
On Sat, 26 Aug 2023 17:24:00 +0200
Translation: I can't do it because its not possible but don't have the balls
to admit I was wrong.

Go back to writing your next piece of lame shit code to post on here in order
to show off.

Bonita Montero

unread,
Aug 26, 2023, 11:39:53 AM8/26/23
to
Of course I can't do that because your requirements are too diffuse.

Tim Rentsch

unread,
Aug 27, 2023, 12:26:52 AM8/27/23
to
Tiib <oot...@hot.ee> writes:

> Double dispatch was first perhaps described by Dan Ingalls in
> seventies

Do you have a reference for that?

> in context of SmallTalk ... [...]

It's Smalltalk, not SmallTalk.

Öö Tiib

unread,
Aug 27, 2023, 1:57:26 AM8/27/23
to
On Sunday, 27 August 2023 at 07:26:52 UTC+3, Tim Rentsch wrote:
> Tiib <oot...@hot.ee> writes:
>
> > Double dispatch was first perhaps described by Dan Ingalls in
> > seventies
>
> Do you have a reference for that?
>
No ... GitHub contains one from eighties; can be I misremembered:
<https://algoritmos-iii.github.io/assets/bibliografia/simple-technique-for-handling-multiple-polymorphism.pdf>

wij

unread,
Aug 27, 2023, 4:13:36 PM8/27/23
to
On Sunday, August 20, 2023 at 10:22:48 PM UTC+8, Öö Tiib wrote:
>...
> Unsure ... people's mind does not grasp double dispatch too well.

This is because people's mind is not depending on such academical concept 'double dispatch'.
I think many people might be interested in the visitor pattern. So, this is my
understanding from a usecase of virtual class, e.g.

class class Element {
public:
virtual void act(Actor &a) = 0; // act==accept
};

class ThingA : public Element { /* omitted */ }
class ThingB : public Element { /* omitted */ }
class ThingC : public Element { /* omitted */ }

int main() {
// The usecase of Element family should normally for something like the list[] below:
Element *list[3]= { new ThingA, new ThingB, new ThingC };

// By now, because only the act(..) member is available, the common usage
// pattern is like the following tow 'for' loops:

for(int i=0; i<3; ++i) { // Ask elements in list[] to do someting (SomeAct1)
list[i]->act(SomeAct1);
}

for(int i=0; i<3; ++i) { // Ask elements in list[] to do someting (SomeAct2)
list[i]->act(SomeAct2);
}

// That something SomeAct1,SomeAct2 are derived from Visitor and defined by user
// This is main point of Visitor Pattern, separates the implement of Element and SomeAct.
}

Öö Tiib

unread,
Aug 28, 2023, 3:14:04 AM8/28/23
to
On Sunday, 27 August 2023 at 23:13:36 UTC+3, wij wrote:
> On Sunday, August 20, 2023 at 10:22:48 PM UTC+8, Öö Tiib wrote:
> >...
> > Unsure ... people's mind does not grasp double dispatch too well.
>
> This is because people's mind is not depending on such academical concept 'double dispatch'.
> I think many people might be interested in the visitor pattern. So, this is my
> understanding from a usecase of virtual class, e.g.
>
Yes, your understanding of use-case is correct, just that the data hierarchy
of a program is usually not a flat list. It is usually something like tree or directed
graph without cycles.

For example list of devices (of different types), every device has list of parameters
(of different types) and list of optional submodules/peripherals connected (of
different types) that may also have options and parameters.

Also visitor is usually used when it is not about operation on one element but needs
to do some operation over all that tree or some subtree of it.

So for above example about devices the visitors can be like that ...
One visitor wants to visit all that data to find out total current and maximum power
consumption of configuration of devices.
Second visitor wants to figure on what temperatures the devices and peripherals
operate.
Third wants to find what kind of backup devices need to be in warehouse for
urgently fix the setup.
Fourth wants to store all current configurable parameter values of the set.
Fifth wants to compose command sequence to turn all the devices off in
fastest and safest manner.

Tim Rentsch

unread,
Aug 28, 2023, 6:07:54 PM8/28/23
to
Tiib <oot...@hot.ee> writes:

> On Sunday, 27 August 2023 at 07:26:52 UTC+3, Tim Rentsch wrote:
>
>> Tiib <oot...@hot.ee> writes:
>>
>>> Double dispatch was first perhaps described by Dan Ingalls in
>>> seventies
>>
>> Do you have a reference for that?
>
> No ... GitHub contains one from eighties; can be I misremembered:
> <https://algoritmos-iii.github.io/assets/bibliografia/
> simple-technique-for-handling-multiple-polymorphism.pdf>

Thank you, that's a good reference.

wij

unread,
Aug 29, 2023, 2:18:47 AM8/29/23
to
I am writing a stave music parser program. Stave music is actually a small
script language similar to average programming language, there is conditional
control flow and stave's 'environment' need to be traced. I am happy the
visitor pattern is learnt and used immediately. The parser part is reduced form
complex 10k lines of codes to 2k simple codes and done fast, just these several days.

In my case, the list[] array structure is enough since this part of program is
just a parser. The tree,graph,... structure of the 'Element' is left for user
to interpret (the visitor can be a printer or different sound devices).

Bonita Montero

unread,
Aug 29, 2023, 1:28:02 PM8/29/23
to
I think that's my final solution. Currently I store the bits in
size_ts and I look up the next set bit with countr_zero which maps
to TZCNT on x64 and according to some small code on godbolt ARM
also has a proper instruction for that.
Resettig the bits for the composites is done by a rotating bit-mask.
If the prime is less than size_t * 8 there's a special case optimi-
zation.

#include <iostream>
#include <vector>
#include <charconv>
#include <string_view>
#include <algorithm>
#include <fstream>
#include <cctype>
#include <cstring>
#include <bit>

#if defined(_MSC_VER)
#pragma warning(disable: 26495) // uninitialized member
#endif
#if defined(__llvm__)
#pragma clang diagnostic ignored "-Wdangling-else"
#endif

using namespace std;

int main( int argc, char **argv )
{
constexpr size_t BUF_SIZE = 0x100000;
try
{
size_t end;
if( argc < 2 )
return EXIT_FAILURE;
char const *sizeStr = argv[1];
bool hex = sizeStr[0] == '0' && (sizeStr[1] == 'x' || sizeStr[1] == 'X');
sizeStr += 2 * hex;
if( from_chars_result fcr = from_chars( sizeStr, sizeStr + strlen(
sizeStr ), end, !hex ? 10 : 16 ); (bool)fcr.ec || *fcr.ptr )
return EXIT_FAILURE;
if( !++end )
throw bad_alloc();
using word_t = size_t;
constexpr size_t BITS = sizeof(word_t) * 8;
size_t bEnd = (end + BITS - 1) & -(ptrdiff_t)BITS;
if( bEnd < end )
throw bad_alloc();
union ndi_word_t { word_t w; ndi_word_t( word_t w ) : w( w ) {} };
using bit_vec = vector<ndi_word_t>;
using bit_it = bit_vec::iterator;
bit_vec sieve( (end + BITS - 1) / BITS, (word_t)0xAAAAAAAAAAAAAAAAu );
ofstream ofs;
union ndi_t { char c; ndi_t() {} };
using ndi_vec = vector<ndi_t>;
constexpr size_t AHEAD = 32;
ndi_vec printBuf;
ndi_vec::iterator bufBegin, bufEnd, bufFlush;
if( argc < 3 || *argv[2] )
{
ofs.exceptions( ofstream::failbit | ofstream::badbit );
ofs.open( argc >= 3 ? argv[2] : "primes.txt", ofstream::trunc );
printBuf.resize( BUF_SIZE + AHEAD - 1 );
bufBegin = printBuf.begin();
bufEnd = bufBegin;
bufFlush = bufBegin + BUF_SIZE;
}
auto print = [&]() { ofs << string_view( &to_address( bufBegin )->c,
&to_address( bufEnd )->c ); };
bit_it const pSieveEnd = sieve.end();
size_t sqr = (ptrdiff_t)ceil( sqrt( (ptrdiff_t)end ) );
for( size_t p = 2; p < end; )
{
if( ofs.is_open() )
{
auto [ptr, ec] = to_chars( &bufEnd->c, &to_address( bufEnd + (AHEAD
- 1) )->c, p );
if( (bool)ec ) [[unlikely]]
throw system_error( (int)ec, generic_category(), "converson failed" );
bufEnd = ptr - &bufBegin->c + bufBegin; // NOP
bufEnd++->c = '\n';
if( bufEnd >= bufFlush )
ofs << string_view( &to_address( bufBegin )->c, &to_address( bufEnd
)->c ),
bufEnd = bufBegin;
}
if( ++p == end ) [[unlikely]]
break;
for( bit_it pSieve = sieve.begin() + p / BITS; ; )
{
size_t g = countr_zero( pSieve->w & (word_t)-1 << p % BITS );
p = (p & -(ptrdiff_t)BITS) + g;
if( g != BITS )
break;
if( ++pSieve == pSieveEnd )
goto end;
}
bit_it pSieve = sieve.begin();
if( size_t m = p * p; p < sqr )
if( p >= BITS ) [[likely]]
{
word_t mask = rotl( (word_t)-2, m % BITS );
do
pSieve[m / BITS].w &= mask,
mask = rotl( mask, (int)p % BITS ),
m += p;
while( m < end );
}
else
{
pSieve += m / BITS;
word_t mask = rotl( (word_t)-2, m % BITS );
do
{
word_t punch = -1, oldMask;
do
punch &= mask,
oldMask = mask,
mask = rotl( mask, (int)p );
while( mask < oldMask );
pSieve->w &= punch;
} while( ++pSieve != pSieveEnd );
}
}
end:
if( ofs.is_open() )
print();
return EXIT_SUCCESS;
}
catch( bad_alloc const & )
{
cout << "out of memory" << endl;
}
catch( ios_base::failure const & )
{
cout << "I/O error" << endl;
}
catch( system_error const &se )
{
cout << se.what() << endl;
}
}

Pavel

unread,
Aug 29, 2023, 9:44:56 PM8/29/23
to
Hard to believe but they were thinking of it as recently as Sep 2020,
see https://wg21.link/p1371r3

But of course, on mutable objects, pattern mathcing loses some 90% of
its use.


Pavel

unread,
Aug 29, 2023, 10:14:33 PM8/29/23
to
Bonita Montero wrote:
> Am 26.08.2023 um 07:05 schrieb Pavel:
>
>> Visitor does not need anything, it simply solves a particular class
>> of  problems that happens to include a structure of objects of
>> multiple types. Your visitor implementation is invalid if it cannot
>> solve a valid visitor problem.
>
> Either I didn't describe the difference to what you imagine correctly
> or you just didn't understand me.
None of the above it is just you are not trying to read.

> In the Visitor pattern, the Visitor
> needs a base class with a virtual method to derive from, and the derived
> object is passed by reference to the Visitor function of the objects
> being visited.
No. Visitor needs no types. The **problems that Visitor solves** include
data structures of multiple types. The applicability of Visitor is
exactly when the programmer is limited to small backward-compatible
changes to the type hierarchy **given in the problem, not "needed by
Visitor"**, ideally, no changes at all.

Try to read what's written, not what you want to see.

I will try one more time, in negative terms, in case it makes it easier
for you to comprehend:

No type hierarchy in the problem -- no need in Visitor in the solution.

Now, "a base class with a virtual method to derive from" are just
possible implementation details for the type hierarchy, so not 100%
needed. The type hierarchy could be a variant or another type hierarchy
where the dynamic type is detectable at run-time. There must be,
however, a comprehensive type hierarchy for the Visitor to be useful.

What you are trying to demonstrate, on the other hand, is called a
"higher-order function" in functional programming. And, no, it does not
need a new name because it already has an industry-standard name.

Bonita Montero

unread,
Aug 30, 2023, 12:14:00 AM8/30/23
to
Am 30.08.2023 um 04:14 schrieb Pavel:

> No. Visitor needs no types. The **problems that Visitor solves** include
> data structures of multiple types. The applicability of Visitor is
> exactly when the programmer is limited to small backward-compatible
> changes to the type hierarchy **given in the problem, not "needed by
> Visitor"**, ideally, no changes at all.

You simply don't understand what I wrote.

Pavel

unread,
Aug 30, 2023, 11:18:21 PM8/30/23
to
Of course if you say so.
0 new messages