Let the data structure be anything (Array, Vectors, Std::Variant). But what is the difference in traversing using just for/for_each against std::visit.
for (auto& Element: collection){
std::visit([](auto arg){std::cout << arg << " ";}, Element);
}
And
for_each(collection.begin(), collection.end(), [](Element& e)
{
std::cout << e;
});
Note1: I know C-style for loop vs C++ for_each..For_each avoids typo error/syntactic sugar. so we can be happy with just for_each.
Note2: I know visitor design pattern too, which will be used for polymorphic object against polymorphic behaviour.
But still I couldn't appreciate and understand std::visit.
And when I digged further, I came to know something called Overload pattern.
template<typename ... Ts>
struct Overload : Ts ... {
using Ts::operator() ...;
};
template<class... Ts> Overload(Ts...) -> Overload<Ts...>;
But this Overload pattern too can be done using the same for_each right?
Thanks in advance for the reply.,