title: Trying to return the value from std::variant using std::visit and a lambda expression
body: <p>Suppose there exists a variant v defined as follows:</p>
  
  <pre><code>std::variant&lt;int,char,double,bool,std::string&gt; v;
  </code></pre>
  
  <p>I am trying to get the underlying value from a std::variant using std::visit or std::get.</p>
  
  <p>I tried doing this naively:</p>
  
  <pre><code>constexpr size_t idx = v.index();
  auto k = std::get&lt;idx&gt;(v);
  </code></pre>
  
  <p>But then learned that this will fail if the variant v is not a constexpr itself. And even then there may be problems with using std::string (due to the definition of the destructor for std::string).</p>
  
  <p>My 2nd attempt was to try and do the following:</p>
  
  <pre><code>auto k = std::visit([](auto arg){return arg;}, v);
  </code></pre>
  
  <p>But received this:</p>
  
  <pre><code>$g++ -o main *.cpp --std=c++17
  In file included from main.cpp:5:0:
  /usr/include/c++/7/variant: In instantiation of ‘static constexpr auto std::__detail::__variant::__gen_vtable_impl&lt;std::__detail::__variant::_Multi_array&lt;_Result_type (*)(_Visitor, _Variants ...)&gt;, std::tuple&lt;_Tail ...&gt;, std::integer_sequence&lt;long unsigned int, __indices ...&gt; &gt;::_S_apply() [with _Result_type = int; _Visitor = main()::&lt;lambda(auto:1)&gt;&amp;&amp;; _Variants = {std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;}; long unsigned int ...__indices = {1}]’:
  /usr/include/c++/7/variant:663:61:   required from ‘static constexpr void std::__detail::__variant::__gen_vtable_impl&lt;std::__detail::__variant::_Multi_array&lt;_Result_type (*)(_Visitor, _Variants ...), __dimensions ...&gt;, std::tuple&lt;_Variants ...&gt;, std::integer_sequence&lt;long unsigned int, __indices ...&gt; &gt;::_S_apply_single_alt(_Tp&amp;) [with long unsigned int __index = 1; _Tp = std::__detail::__variant::_Multi_array&lt;int (*)(main()::&lt;lambda(auto:1)&gt;&amp;&amp;, std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;)&gt;; _Result_type = int; _Visitor = main()::&lt;lambda(auto:1)&gt;&amp;&amp;; long unsigned int ...__dimensions = {5}; _Variants = {std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;}; long unsigned int ...__indices = {}]’
  /usr/include/c++/7/variant:651:39:   required from ‘constexpr const std::__detail::__variant::_Multi_array&lt;int (*)(main()::&lt;lambda(auto:1)&gt;&amp;&amp;, std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;), 5&gt; std::__detail::__variant::__gen_vtable&lt;int, main()::&lt;lambda(auto:1)&gt;&amp;&amp;, std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;&gt;::_S_vtable’
  /usr/include/c++/7/variant:704:29:   required from ‘struct std::__detail::__variant::__gen_vtable&lt;int, main()::&lt;lambda(auto:1)&gt;&amp;&amp;, std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;&gt;’
  /usr/include/c++/7/variant:1239:23:   required from ‘constexpr decltype(auto) std::visit(_Visitor&amp;&amp;, _Variants&amp;&amp; ...) [with _Visitor = main()::&lt;lambda(auto:1)&gt;; _Variants = {std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;}]’
  main.cpp:89:49:   required from here
  /usr/include/c++/7/variant:704:49:   in constexpr expansion of ‘std::__detail::__variant::__gen_vtable&lt;int, main()::&lt;lambda(auto:1)&gt;&amp;&amp;, std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;&gt;::_S_apply()’
  /usr/include/c++/7/variant:701:38:   in constexpr expansion of ‘std::__detail::__variant::__gen_vtable_impl&lt;std::__detail::__variant::_Multi_array&lt;int (*)(main()::&lt;lambda(auto:1)&gt;&amp;&amp;, std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;), 5&gt;, std::tuple&lt;std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;&gt;, std::integer_sequence&lt;long unsigned int&gt; &gt;::_S_apply()’
  /usr/include/c++/7/variant:641:19:   in constexpr expansion of ‘std::__detail::__variant::__gen_vtable_impl&lt;std::__detail::__variant::_Multi_array&lt;int (*)(main()::&lt;lambda(auto:1)&gt;&amp;&amp;, std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;), 5&gt;, std::tuple&lt;std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;&gt;, std::integer_sequence&lt;long unsigned int&gt; &gt;::_S_apply_all_alts&lt;0, 1, 2, 3, 4&gt;(\xe2\x80\x98result_dec\xe2\x80\x99 not supported by dump_expr#&lt;expression error&gt;, (std::make_index_sequence&lt;5&gt;(), std::make_index_sequence&lt;5&gt;()))’
  /usr/include/c++/7/variant:686:43: error: invalid conversion from ‘std::__success_type&lt;char&gt;::type (*)(main()::&lt;lambda(auto:1)&gt;&amp;&amp;, std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;) {aka char (*)(main()::&lt;lambda(auto:1)&gt;&amp;&amp;, std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;)}’ to ‘int (*)(main()::&lt;lambda(auto:1)&gt;&amp;&amp;, std::variant&lt;int, char, double, bool, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;)’ [-fpermissive]
         { return _Array_type{&amp;__visit_invoke}; }
                                             ^
  </code></pre>
  
  <p>I'm stuck as to why the std::visit call does not work. I thought I supplied a trivial lambda expression which takes all possible types for the variant and returns the underlying value but it appears I am misunderstanding something.</p>
  
  <p>I want to use std::variant now (after initially considering std::any (see <a href="https://stackoverflow.com/questions/52085547/avoid-writing-the-same-repetitive-type-checking-code-with-stdany">avoid writing the same repetitive type-checking code with std::any</a>) but I need a way to return the contained value. Any help would be greatly appreciated. Thank you very much.</p>