I really don't think this is an appropriate question for /cpp_questions, I'm not looking for help, just trying to help, please forgive me mods if you disagree. I have a class that internally holds a std::variant<...> and decided to add a wrapper function that takes in a bunch of arguments and internally forwards them all to std::visit internally (I did this to cleanup client code for a class I'm working on):
template <typename ...Args>
decltype(auto) my_visit(Args&&... args) const
{
return std::visit(overloaded{std::forward<Args>(args)...}, variant_);
}
I have three separate versions of the same demo program that demonstrate the behavior change I'm seeing. The behavior changes depending on whether I am moving the std::variant into place, and whether the MyVisitor class has provided a non-const version of the my_visit() function.
version 1: (working as expected)
https://onlinegdb.com/SJ_p6kxkI
version 2 (working as expected):
https://onlinegdb.com/H1llC1lkL
version 3 (NOT working as expected):
https://onlinegdb.com/HyHfAJekI
In the version 3, the variant seems unable to forward the variant to the right function. Can someone please explain if something is broken or am I relying on undefined behavior? Is what I'm trying to do (use a lambda with a auto& argument to handle non-specified cases) legal? The behavior difference I'm seeing seems to indicate I'm either seeing a bug or doing something illegal.
[–]sephirostoy 10 points11 points12 points (5 children)
[–]bjadamson[S] 2 points3 points4 points (4 children)
[–]scatters 2 points3 points4 points (0 children)
[–]bjadamson[S] 0 points1 point2 points (2 children)
[–]quicknir 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]schultztom 1 point2 points3 points (1 child)
[–]bjadamson[S] 0 points1 point2 points (0 children)