Using std::visit with variadic template struct

前端 未结 1 1656
有刺的猬
有刺的猬 2020-12-05 15:43

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

#include 
#include &l         


        
相关标签:
1条回答
  • 2020-12-05 16:30

    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.

    0 讨论(0)
提交回复
热议问题