There is yet another kind of visitor that we need to discuss.
And this has to do with the essay the very type.
So for those of you who are not familiar with the variant and whether it's for let me just show you.
So let's suppose for example that you want to keep a house address.
Now most of the houses just have house numbers you have for example house number 123 at London road.
But some houses actually have names as opposed to numbers so you might want to have this ability of
being able to store one value or the other value.
So how will this work in kind of traditional old C++ shall we say.
Well typically you will do something on the following.
You would have a structure called the house and inside that you would have a union where you would have
a string called House thaim and an integer called house number.
And then you would have some sort of tag.
So you would have gone ENM or indeed in the class which is called type where you would have a specifier.
What kind of type you have here.
So you have either the name of the number and number like so.
And that would be the type of the actual Union.
So in this case this is kind of the old fashioned way of doing things the C way of doing things.
Now in C++ in modern C++ I should say we have a type called variant which originally appeared this part
of boost was boost.
Variant and so we now have this variant type.
Now the thing about the variant is you can specify as template arguments all over the varieties of data
to store two so here we want to store either a string or an integer and this is going to be called House.
So now all we can do is we can assign either the string part or the integer parts of.
For example I can see house equals 123 if that's the house number.
Or alternatively I can say house equals let's say if you buy a car so you're going to have plans or
fury costs for example so that's going to be a string and then only one of those can be assigned an
Access to take time.
Now question is well how can you actually print that part of the House which has actually been assigned
Now it's not just as simple as you know getting some sort of getting up sort of like boolean flag indicating
which type has been assigned or what you need to do instead is you need to build a visitor.
Surprise surprise here is the Visitor pattern once again in the slightly different kind of implementation.
It's not the kind of visitors that we've been building but essentially the idea is the same you want
some sort of component which takes a house and knows how to print the right part of the house.
So typically what has been done in modern C++ for this particular thing is a function called SDD visit.
Now as to the view that you just need the the prefix.
By the way it takes two arguments it takes a callable which is basically the visitor.
So that's the first argument and the second argument is the stuff that you actually want to visit.
So in this case that would be our house variant.
So what is required of such a visitor.
Because as you can see from the signature here it doesn't really help us in any way to figure out what
kind of object is required.
Well let me show you Lesbos you want to have an address printer.
So we're going to print just the house for now so we'll have a structure called a dress printer and
what you need to do is you need to overload the function call operator for every single type of the
verion So here we have two types which have a string and we have an integer.
So we need to overload the function call operator for both of them.
So here you have operator which would take a string house name cons by the way.
And here we can print.
This is a house called so-and-so soci out a house called.
And then house name a line break at the end.
Now alternatively we can get a number in which case once again you have the function call operator which
takes a house number.
This time around and here we see our house number
and they line break the end.
So how do you use this thing while coming back to us to revisit what you have to do first.
You'd have to make a visitor which in our case is the address sprinter's So let's put that in here.
So here we have address printer a.p and then we call as to the visit with AP and house as that thing
which we're actually going to print.
So this is an implementation of a very specific visitor a visitor which works on and as to the variant.
And also you can see that there is a correspondence and here is the output by the way House called me
and if you're a kaso because I've assigned the string part and if I had a signed integer part that would
have been house number 123.
So we have a specialized visitor which just overloads its function call operator and makes all of these
overloads with different arguments and then what happens with Estyn you visit it it actually checks
at compile time that you actually have all of those cases implemented and it complains otherwise it's
going to complain at you that you know this isn't going to work.
Now if you don't want to be making an address printed which certainly feels a lot like a very old fashioned
kind of funk to our approach if you know funk Tor's this feels like a funk or just with lots of overloads.
You can do is you can actually use Lamhe does.
But in this case it gets a bit more tricky so let's suppose that instead of making Andrew as printer
what you decide is you decide to actually construct some sort of lambda here and now you can do this
you can actually pass in a lambda what you need to do is you need to once again check a compile time
what kind of type you're actually getting.
So let me show you how this works so here is my lammed is going to take an argument which I declare
as auto because we don't really know what kind of argument it is.
It could be a string it could be an integer.
Let me actually shift all of this backwards.
It's not convenient to work with it like this.
So here inside the curly braces what we need to do first of all is we need to figure out the type of
the actual argument.
Now we have it as a reference here.
So I'm going to use declarative just just ordinary type T so using T equals D K T with Dekel type of
are now we go.
So that gets us the type.
And then what we can do is we can actually start checking this type against certain varieties.
So for example we know that it could be either a string or if it's in the string then it's obviously
bound to be an end.
So here we can say if Konst exper is same that's actually use is saying the the more fancy approach
so if T is the same as a string that what we can do is we can treat it as a string.
You'll notice that C line is kind of collapsing here because it doesn't know about these constructs
yet so let me just get rid of the both here.
OK so what we do here is we basically replicate this part so the parts with the house called so and
so except the argument name is different.
So here we would stick the argument name of just arc.
Basically instead of.
So it will stick the dog here and then we put the else and the ELSE can only be an integer.
There's really nothing else for us to consider.
So here we get the house number.
So let's stick that in here as well and once again we have to hit ARG instead of whatever we have here.
So despite all of these nasty inspections and see line complaining at just about everything this is
actually valid code in C++ 17.
And it does the same thing as a address printer does except that it does it kind of in line without
you having to construct a type.
So if I just let me change the example I'll get rid of house name and I'll put it in the house number
we can compile it and see that it actually works in this particular case.
All right so as you can see we're using a number here and that's why we're getting house number 123.
So the takeaway from this is that you do get these specialized approaches which say to the different
cases of the Visitor pattern.
In this case it's a specialized approach which specifically supports the variant type.
So you specifically want to support this type which keeps the type list of the possible contents of
that type.
And so you have either the approach of creating a special just type with lots of functors effectively
or you can do it in line with a bit of C++ 17 magic.