I have the following code:
#include <iostream>
#include <string>
#include <variant>
template <class... Ts>
struct overloaded : Ts...
{
using Ts::operator()...;
};
template <class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
template<typename... Fns, typename... Ts>
void xvisit(std::variant<Ts...> var, const Fns... fns ){
std::visit(overloaded{fns...}, var);
}
It seems to be working fine(godbolt), but I can not find a way to make the std::variant<Ts...>
be the last argument of xvisit
.
For example when I swap the order of arguments I get useless errors from both clang and gcc:
note: '<lambda(const string&)>' is not derived from 'std::variant<_Types ...>'
note: candidate template ignored: could not match 'variant<type-parameter-0-1...>' against '(lambda at :23:17)'
godbolt with problematic xvisit2.
Can this be fixed, or am I hitting some language limitation?
note: I know xvisit
should not be returning void
, this is just an example.