1

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.

NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
  • 1
    Your parameter pack should be the last argument. You can't have any other arguments following parameter pack arguments. There should be a duplicate, I am 101% sure. – SergeyA Mar 25 '21 at 18:37
  • 1
    @nathan the problem is that says *why it doesn't work*, it doesn't answer *how to do it*. Because it can be done, it just is a lot more work than you should put into it. Honestly, if your really want trailing variant access, `xvisitor(fns...)(variant)` is probably a better choice (and has the cute syntax that matches `overloaded`). Or even `overloaded{fns...}.visit(variants...)` to make it explicit. – Yakk - Adam Nevraumont Mar 25 '21 at 18:51
  • @Yakk-AdamNevraumont Are you talking about `Parameters after parameter pack in function [duplicate]` or `Variadic function template with pack expansion not in last parameter`? In the latter you have an answer that is a work around. – NathanOliver Mar 25 '21 at 18:53
  • @NathanOliver Ah, I ran into checkmark itis, where I stopped at the checkmark. Yes, that is the kind of bad plan that answers the OP's question more literally. :) – Yakk - Adam Nevraumont Mar 25 '21 at 19:11
  • Thanks everybody, I know of this limitation, but I thought it only applies to template arguments, like is banned, not on the function argument order... – NoSenseEtAl Mar 25 '21 at 19:14

0 Answers0