Being inspired by This example, namely by this particular piece of code
// ...
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
// ...
std::visit(overloaded {
[](auto arg) { std::cout << arg << ' '; },
[](double arg) { std::cout << std::fixed << arg << ' '; },
[](const std::string& arg) { std::cout << std::quoted(arg) << ' '; },
}, v);
... I tried to define a class' member of type overloaded
, but stumbled on difficulties, which I believe (but not exactly sure) have been caused by inability of the compiler to deduce template's parameters.
template<class ...Ts>
struct overloaded : Ts... {
using Ts::operator()...;
constexpr overloaded(Ts... ts) : Ts(ts)... {}
};
struct as{};
class phil_t {
int i;
overloaded o { [&](as&) {cout << i << "as"; } }; // <-- HERE
};
That's the output I receive:
../../variant.hpp:21:5: error: invalid use of template-name ‘overloaded’ without an argument list
overloaded o { [&](as&) {cout << i << "as"; } };
^~~~~~~~~~
Tellingly, when I use the same approach to instantiate an object not inside a class, everything works just smoothly.
void varnt() {
int i = 42;
auto o = overloaded( [&](as&) {cout << i << "as"; } );
}
Any advice, explaination or hint at some workarounds will be enormously appreciated.
Thank you.