<div dir="ltr"><div>Sorry, email went out before intended...</div><div><br></div><div>Maybe easier to troubleshoot than the partial code I posted earlier, I've modifed Peter's Godbolt sample from [1] to replicate (I think) what I need.</div><div>Short and long links below. I'm not familiar with Godbolt, hopefully those links are correct. I'd appreciate some insights on this issue. Thanks, --DD</div><div><br></div><div><a href="https://godbolt.org/z/3xhjnMn5G">https://godbolt.org/z/3xhjnMn5G</a><br></div><div><br></div><div><a href="https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAM1QDsCBlZAQwBtMQBGAFlJvoCqAZ0wAFAB4gA5AAYppAFZdSrZrVDIApACYAQjt2kR7ZATx1KmWugDCqVgFcAtrRDcZpK%2BgAyeWpgA5ZwAjTGIQAFYANlIAB1QhQnNaO0cXNw94xLM6X38gp1Dw6KNMExzaBgJmYgJU51d3UvLkqpqCPMCQsMiYoWra%2BvSm/vbOgqLegEojVAdiZA4pHQBmP2RHLABqTRWbADcavDUCXexNGQBBVfXNzB29837iTGYnM4vrq5vaDYdt3Y2YKoBIEAD0TlinE4ADoELFYh8rg5EuotrQ3pghLFmIstsDQSAQJDobt9N8rgRMJDVFTARtmEIhDCWVsACqkLYMpksmFba4rc5XfroImHYjHeiAtm8s5bJzMADWmAA%2BuLJQQIFsRUTEgAvVUELZ%2BLDiTnXbRRHRRXlbZhbKafTQAdnJly2Hq2LwI81o8tiKoA7oQECqTZhxID9ZhUFReRA2Y7BVrw2adhF9JbNBEACIQZgOIhbACSjtdTqunqrXswPuIfp1IHVJ2lsuT2oIopAfhVsVUizD1gjgOLZ05jZoxEDNXQgIF2C19qmtqTbs%2BVZdOdXTudOYrlypNOYdL23OZrLZSMuBaLfkIaqOJy1jejKqNqfHnbFj6lexlLI%2BS0tn2R1vnLStPW9X1/SDENB1NKM8ANWN40TM4UyHNNs0zKJ9mzPMb1QEsyzXCDqxrOs/X2GFqT7XFMBHdDtwpd1PU3Zjrl3fcwR43i%2BP4gTBKE4SRNEsSeM%2BHitip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01AMpcpb%2BpWo7OA22h71qzVOHmg7VthKaztaxzSqkGZWGkCJ5FcWR5FQaRzOivRtTmBZ7lWTh5AIaQ5CmGZFRAFZuBhThtGdFZnUtGQZG0GQoiiAAOFRpG4eQnC4InSGBuRSDBqR5CEEAPCRkGZjgWAkDQSE8HYMgKAgQXYmFnpgAATm0PhhapYgOYgYJkfkYI/BqABPaQEdIQWnCsAgAHlaFYXWQdILAFXUdh1etvAXlMPB9ixB2I0wZBCyWZmUrKB3WDwYJiB1uwsD1xGJTpqQEZmfhGBYe2eD4OgCGEMRJCtpRntUdQUAMAwVGDjnYAxI2QAJfpSDd8JY%2BdOOZlQWIKg5qQAFoRV2HMtEh3ROGdLZ25N7R2bKL2KksawhlcZ6vHGbpwmerIkjoGflBXioF8KHpnuMCfWgGOp7AaZR95dug2lqbfJj3o/17vsY/C6Hel5mIQYcWLg/oBoGHdZ8Q5MojtyiNwLYwBkDIC2DLGE2gtgQFwIQEglkVjPS2HYIWIsUGcFGhDTMuhEbq1RqQdGVkYQRBluTFYEQrJWRlpwcmlCqZSBpozf%2B0h2ac1INzFGpA%2BaIBAGFWIhZyCUAllLJenh8BEEkQnJgbAOApwThnCQDtAyh1iJHH%2BUhAZsKtqzE2hZhFGljFsQBwDQHgMgdA2B8CMGSywfDKYhCeZozcCsWBzpuC4wiAPCmXj8bMNYUzUGHCjBcJ4b9bRo89HM1Zi4lGMxa6JAsNwIAA%3D</a><br></div><div><br></div><div>Just in case, here's the code (I modified to add init_variant and the last two lines in main, and this does not compile) inline:<br></div><div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254);font-family:Consolas,"Liberation Mono",Courier,monospace,Consolas,"Courier New",monospace;font-size:14px;line-height:19px;white-space:pre"><div><span style="color:rgb(0,0,255)">#include</span> <span style="color:rgb(0,0,255)"><</span><span style="color:rgb(163,21,21)">variant</span><span style="color:rgb(0,0,255)">></span></div><div><span style="color:rgb(0,0,255)">#include</span> <span style="color:rgb(0,0,255)"><</span><span style="color:rgb(163,21,21)">iostream</span><span style="color:rgb(0,0,255)">></span></div><br><div><span style="color:rgb(0,0,255)">#include</span> <span style="color:rgb(0,0,255)"><</span><span style="color:rgb(163,21,21)">boost/mp11.hpp</span><span style="color:rgb(0,0,255)">></span></div><div><span style="color:rgb(0,0,255)">using</span> <span style="color:rgb(0,0,255)">namespace</span> boost::mp11;</div><br><div><span style="color:rgb(0,0,255)">template</span><<span style="color:rgb(0,0,255)">class</span>... T, <span style="color:rgb(0,0,255)">class</span>... A></div><div>std::variant<T...> make_variant( std::size_t index, A&&... a )</div><div>{</div><div> <span style="color:rgb(0,0,255)">return</span> mp_with_index<<span style="color:rgb(0,0,255)">sizeof</span>...(T)>( index, [&](<span style="color:rgb(0,0,255)">auto</span> I){</div><br><div> <span style="color:rgb(0,0,255)">return</span> std::variant<T...>( std::in_place_index<I>, std::forward<A>( a )... );</div><br><div> });</div><div>}</div><br><div><span style="color:rgb(0,0,255)">template</span><<span style="color:rgb(0,0,255)">class</span>... T></div><div><span style="color:rgb(0,0,255)">auto</span> init_variant( std::size_t index, std::variant<T...>& v)</div><div>{</div><div> <span style="color:rgb(0,0,255)">return</span> mp_with_index<<span style="color:rgb(0,0,255)">sizeof</span>...(T)>( index, [&v](<span style="color:rgb(0,0,255)">auto</span> I){</div><br><div> <span style="color:rgb(0,0,255)">return</span> v.emplace<I>();</div><br><div> });</div><div>}</div><br><div><span style="color:rgb(0,128,0)">////////////////////////////////////////////////////////////////////////////////</span></div><div><span style="color:rgb(0,128,0)">// test</span></div><br><div><span style="color:rgb(0,0,255)">struct</span> A</div><div>{</div><div> <span style="color:rgb(0,0,255)">void</span> info()</div><div> {</div><div> std::cout << <span style="color:rgb(163,21,21)">"A"</span>;</div><div> }</div><div>};</div><br><div><span style="color:rgb(0,0,255)">struct</span> B</div><div>{</div><div> <span style="color:rgb(0,0,255)">void</span> info()</div><div> {</div><div> std::cout << <span style="color:rgb(163,21,21)">"B"</span>;</div><div> }</div><div>};</div><br><br><div><span style="color:rgb(0,0,255)">int</span> main()</div><div>{</div><div> std::size_t index = <span style="color:rgb(9,134,88)">1</span>;</div><br><div> <span style="color:rgb(0,0,255)">auto</span> v = make_variant<A, B>(index);</div><br><div> std::visit([](<span style="color:rgb(0,0,255)">auto</span>&& o) { <a href="http://o.info">o.info</a>(); }, v);</div><br><div> <span style="color:rgb(0,0,255)">auto</span>& alt0 = init_variant(<span style="color:rgb(9,134,88)">0</span>, v); <a href="http://alt0.info">alt0.info</a>();</div><div> <span style="color:rgb(0,0,255)">auto</span>& alt1 = init_variant(<span style="color:rgb(9,134,88)">1</span>, v); <a href="http://alt1.info">alt1.info</a>();</div><div>}</div><br></div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254);font-family:Consolas,"Liberation Mono",Courier,monospace,Consolas,"Courier New",monospace;font-size:14px;line-height:19px;white-space:pre"><br></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 30, 2021 at 3:27 PM Dominique Devienne <<a href="mailto:ddevienne@gmail.com">ddevienne@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi. I'm modernizing some code, to use std::variant instead of ad-hoc peudo-variant structs.<div>These structs need to be encoded/decoded via avro::codec_traits specializations, but given</div><div>that std::variant is variadic, I'm struggling a bit. I've done my research and found peter's [1] and also looked at [2], which I thought might work, but does not, and I don't understand the errors I'm getting with MSVC 2019 in C++17 mode.</div><div><br></div><div>The code is below, with also a subset of the compiler errors I'm getting.</div><div><br></div><div><br></div><div>[1] <a href="https://www.reddit.com/r/cpp/comments/f8cbzs/creating_stdvariant_based_on_index_at_runtime/filw8g7?utm_source=share&utm_medium=web2x&context=3" target="_blank">https://www.reddit.com/r/cpp/comments/f8cbzs/creating_stdvariant_based_on_index_at_runtime/filw8g7?utm_source=share&utm_medium=web2x&context=3</a></div><div>[2] <a href="https://www.boost.org/doc/libs/develop/libs/mp11/doc/html/mp11.html#mp_with_indexni_f" target="_blank">https://www.boost.org/doc/libs/develop/libs/mp11/doc/html/mp11.html#mp_with_indexni_f</a><br><div><br></div><div>template <> struct codec_traits<std::monostate> {<br> static void encode(Encoder& e, const std::monostate&) {<br> e.encodeNull();<br> }<br><br> static void decode(Decoder& d, std::monostate&) {<br> d.decodeNull();<br> }<br>};<br><br>template <typename... Ts> struct codec_traits<std::variant<Ts...>> {<br> static void encode(Encoder& e, const std::variant<Ts...>& var) {<br> e.encodeUnionIndex(var.index());<br> std::visit(<br> [&e](const auto& alternative) {<br> avro::encode(e, alternative);<br> },<br> var<br> );<br> }<br><br> static void decode(Decoder& d, std::variant<Ts...>& var) {<br> const size_t index = d.decodeUnionIndex();<br> if (index >= sizeof...(Ts)) {<br> throw avro::Exception("Invalid Union index");<br> }<br> auto& alternative =<br> boost::mp11::mp_with_index<sizeof...(Ts)>(<br> index,<br> [&var](auto I) {<br> return var.emplace<I>(); // I is mp_size_t<index>{} here<br> }<br> )<br> ;<br> avro::decode(d, alternative);<br> }<br>};<br><br></div></div><div><br></div><div>1>Messages.cpp<br>1>D:\pdgm\trunk\psc3\SharedComponents\src\lib\pdgm\eml\etp12\Messages.hpp(70,1): error C2220: the following warning is treated as an error<br>1>D:\pdgm\trunk\psc3\SharedComponents\src\lib\pdgm\eml\etp12\Messages.hpp(70,1): warning C4239: nonstandard extension used: 'initializing': conversion from 'std::monostate' to 'std::monostate &'<br>1>D:\pdgm\trunk\psc3\SharedComponents\src\lib\pdgm\eml\etp12\Messages.hpp(70,1): message : A non-const reference may only be bound to an lvalue<br>1>D:\pdgm\trunk\psc3\SharedComponents\src\lib\pdgm\eml\etp12\Messages.hpp(70): message : while compiling class template member function 'void avro::codec_traits<T>::decode(avro::Decoder &,std::variant<std::monostate,int64_t,double> &)'<br>1> with<br>1> [<br>1> T=pdgm::eml::etp12::Datatypes::IndexValue_item_v<br>1> ]<br>1>D:\pdgm\kits\trunk\Avrocpp\1.9.2-Boost-1.74.0\Win_x64_10_v16_debug\include\avro/Specific.hh(341): message : see reference to function template instantiation 'void avro::codec_traits<T>::decode(avro::Decoder &,std::variant<std::monostate,int64_t,double> &)' being compiled<br>1> with<br>1> [<br>1> T=pdgm::eml::etp12::Datatypes::IndexValue_item_v<br>1> ]<br>1>D:\pdgm\kits\trunk\Avrocpp\1.9.2-Boost-1.74.0\Win_x64_10_v16_debug\include\avro/Specific.hh(333): message : see reference to class template instantiation 'avro::codec_traits<T>' being compiled<br>1> with<br>1> [<br>1> T=pdgm::eml::etp12::Datatypes::IndexValue_item_v<br>1> ]<br>1>D:\pdgm\trunk\psc3\SharedComponents\src\lib\pdgm\eml\etp12\codegen/Messages.i.ipp(1527): message : see reference to function template instantiation 'void avro::encode<pdgm::eml::etp12::Datatypes::IndexValue_item_v>(avro::Encoder &,const T &)' being compiled<br>1> with<br>1> [<br>1> T=pdgm::eml::etp12::Datatypes::IndexValue_item_v<br>1> ]<br></div><div><br></div><div><br></div><div>1>D:\pdgm\kits\trunk\boost\1.74.0\Win_x64_10_v16_debug\include\boost/mp11/detail/mp_with_index.hpp(87,1): error C2440: 'return': cannot convert from '__int64' to 'std::monostate'<br>1>D:\pdgm\kits\trunk\boost\1.74.0\Win_x64_10_v16_debug\include\boost/mp11/detail/mp_with_index.hpp(87,1): message : No constructor could take the source type, or constructor overload resolution was ambiguous<br>1>D:\pdgm\kits\trunk\boost\1.74.0\Win_x64_10_v16_debug\include\boost/mp11/detail/mp_with_index.hpp(371): message : see reference to function template instantiation 'std::monostate boost::mp11::detail::mp_with_index_impl_<3>::call<0,_Ty>(size_t,F &&)' being compiled<br>1> with<br>1> [<br>1> _Ty=avro::codec_traits<pdgm::eml::etp12::Datatypes::IndexValue_item_v>::decode::<lambda_7e593df011d98e42859c6ecb357b3e3f>,<br>1> F=avro::codec_traits<pdgm::eml::etp12::Datatypes::IndexValue_item_v>::decode::<lambda_7e593df011d98e42859c6ecb357b3e3f><br>1> ]<br>1>D:\pdgm\trunk\psc3\SharedComponents\src\lib\pdgm\eml\etp12\Messages.hpp(70): message : see reference to function template instantiation 'std::monostate boost::mp11::mp_with_index<3,avro::codec_traits<T>::decode::<lambda_7e593df011d98e42859c6ecb357b3e3f>>(size_t,F &&)' being compiled<br>1> with<br>1> [<br>1> T=pdgm::eml::etp12::Datatypes::IndexValue_item_v,<br>1> F=avro::codec_traits<pdgm::eml::etp12::Datatypes::IndexValue_item_v>::decode::<lambda_7e593df011d98e42859c6ecb357b3e3f><br>1> ]<br>1>D:\pdgm\trunk\psc3\SharedComponents\src\lib\pdgm\eml\etp12\Messages.hpp(70): message : while compiling class template member function 'void avro::codec_traits<T>::decode(avro::Decoder &,std::variant<std::monostate,int64_t,double> &)'<br>1> with<br>1> [<br>1> T=pdgm::eml::etp12::Datatypes::IndexValue_item_v<br>1> ]<br>1>D:\pdgm\kits\trunk\Avrocpp\1.9.2-Boost-1.74.0\Win_x64_10_v16_debug\include\avro/Specific.hh(341): message : see reference to function template instantiation 'void avro::codec_traits<T>::decode(avro::Decoder &,std::variant<std::monostate,int64_t,double> &)' being compiled<br>1> with<br>1> [<br>1> T=pdgm::eml::etp12::Datatypes::IndexValue_item_v<br>1> ]<br></div></div> </blockquote></div></div></div>