Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using std::visit with variadic template struct

Tags:

c++

c++17

I was trying to understand the following example which I got from http://en.cppreference.com/w/cpp/utility/variant/visit

#include <iomanip>
#include <iostream>
#include <string>
#include <type_traits>
#include <variant>
#include <vector>


using var_t = std::variant<int, long, double, std::string>;

template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
// what is this declaration imply???
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

int main() {
    std::vector<var_t> vec = {10, 15l, 1.5, "hello"};


    for (auto& v: vec) {
        std::visit(overloaded {
            [](auto arg) { std::cout << arg << '\n'; },
            [](double arg) { std::cout << std::fixed << arg << '\n'; },
            [](const std::string& arg) { std::cout << std::quoted(arg) << '\n'; },
        }, v);
    }
}

Can someone please explain how this overloaded struct works? Especially what I didn't understand is the following declaration.

template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

Without this declaration, the compiler issues the following error messages.

main.cpp: In function 'int main()':
main.cpp:26:9: error: class template argument deduction failed:
         }, v);
         ^
main.cpp:26: confused by earlier errors, bailing out

Purpose: learning

like image 297
Paul Varghese Avatar asked Jul 10 '17 19:07

Paul Varghese


1 Answers

Can someone please explain how this overloaded struct works? Especially what I didn't understand is the following declaration.

template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

That's an user-defined deduction guide (link to the working draft).
It's a feature of the language introduced by the latest revision of the standard along with class template arguments deduction. See also here for more details and a more user-friendly explanation.
This is not a proper explanation, but for the sake of simplicity you can consider it as an hint you can give to lead the deduction of the template arguments out of a set of parameters given to the constructor.


As a side note, here I found an example that is pretty clear and it's worth copying it over:

template<typename T>
struct Thingy { T t; };

Thingy(const char *) -> Thingy<std::string>;

// ...

Thingy thing{"A String"}; // thing.t is a `std::string`.

Credits are for @NicolBolas, an active user here on SO. Unfortunately I can't find the answer from which this example has been taken.

like image 146
skypjack Avatar answered Sep 23 '22 15:09

skypjack