对可变参数模板结构使用std :: visit [英] Using std::visit with variadic template struct

查看:137
本文介绍了对可变参数模板结构使用std :: visit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解以下示例,该示例来自 http:// en.cppreference.com/w/cpp/utility/variant/visit

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

推荐答案


有人可以解释一下这个重载的结构如何工作吗?尤其是我不理解的是以下声明。

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`.

信用额用于@ NicolBolas,SO上的活跃用户。不幸的是,我找不到此示例的答案。

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

这篇关于对可变参数模板结构使用std :: visit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆