Using std::visit with variadic template struct

限于喜欢 提交于 2019-11-28 08:30:42

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!