Boost logo

Boost Users :

From: Dominique Devienne (ddevienne_at_[hidden])
Date: 2021-06-30 13:44:47


Sorry, email went out before intended...

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

https://godbolt.org/z/3xhjnMn5G

https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAM1QDsCBlZAQwBtMQBGAFlJvoCqAZ0wAFAB4gA5AAYppAFZdSrZrVDIApACYAQjt2kR7ZATx1KmWugDCqVgFcAtrRDcZpK%2BgAyeWpgA5ZwAjTGIQAFYANlIAB1QhQnNaO0cXNw94xLM6X38gp1Dw6KNMExzaBgJmYgJU51d3UvLkqpqCPMCQsMiYoWra%2BvSm/vbOgqLegEojVAdiZA4pHQBmP2RHLABqTRWbADcavDUCXexNGQBBVfXNzB29837iTGYnM4vrq5vaDYdt3Y2YKoBIEAD0TlinE4ADoELFYh8rg5EuotrQ3pghLFmIstsDQSAQJDobt9N8rgRMJDVFTARtmEIhDCWVsACqkLYMpksmFba4rc5XfroImHYjHeiAtm8s5bJzMADWmAA%2BuLJQQIFsRUTEgAvVUELZ%2BLDiTnXbRRHRRXlbZhbKafTQAdnJly2Hq2LwI81o8tiKoA7oQECqTZhxID9ZhUFReRA2Y7BVrw2adhF9JbNBEACIQZgOIhbACSjtdTqunqrXswPuIfp1IHVJ2lsuT2oIopAfhVsVUizD1gjgOLZ05jZoxEDNXQgIF2C19qmtqTbs%2BVZdOdXTudOYrlypNOYdL23OZrLZSMuBaLfkIaqOJy1jejKqNqfHnbFj6lexlLI%2BS0tn2R1vnLStPW9X1/SDENB1NKM8ANWN40TM4UyHNNs0zKJ9mzPMb1QEsyzXCDqxrOs/X2GFqT7XFMBHdDtwpd1PU3Zjrl3fcwR43i%2BP4gTBKE4SRNEsSeM%2BHitipfp92eBxTH5Hc3Q9fZUDwdBjVoGgIFA1iXRUz1GzQQsHhsQEdm0bQLW0Ml1x2LiwL3FZSMueTFLXcDWLUjStJ0vSPQM%2ByPWMuYjUBCydD0KK7Igzcd2c1zPj8I0FT8XTlPsl8kMNLTTQeHMtk4WKvlYwjgIK%2BUlVVZtfxsS5OX0ZNUw4rKvybPBsggbD8PzQtUGta0tlQKYHN0YaYT8fyyQcnNORAkr7MIoa2AIGRKrvAgHwlJ8PGA1c7VYNbJu01AMpcpb%2BpWo7OA22h71qzVOHmg7VthKaztaxzSqkGZWGkCJ5FcWR5FQaRzOivRtTmBZ7lWTh5AIaQ5CmGZFRAFZuBhThtGdFZnUtGQZG0GQoiiAAOFRpG4eQnC4InSGBuRSDBqR5CEEAPCRkGZjgWAkDQSE8HYMgKAgQXYmFnpgAATm0PhhapYgOYgYJkfkYI/BqABPaQEdIQWnCsAgAHlaFYXWQdILAFXUdh1etvAXlMPB9ixB2I0wZBCyWZmUrKB3WDwYJiB1uwsD1xGJTpqQEZmfhGBYe2eD4OgCGEMRJCtpRntUdQUAMAwVGDjnYAxI2QAJfpSDd8JY%2BdOOZlQWIKg5qQAFoRV2HMtEh3ROGdLZ25N7R2bKL2KksawhlcZ6vHGbpwmerIkjoGflBXioF8KHpnuMCfWgGOp7AaZR95dug2lqbfJj3o/17vsY/C6Hel5mIQYcWLg/oBoGHdZ8Q5MojtyiNwLYwBkDIC2DLGE2gtgQFwIQEglkVjPS2HYIWIsUGcFGhDTMuhEbq1RqQdGVkYQRBluTFYEQrJWRlpwcmlCqZSBpozf%2B0h2ac1INzFGpA%2BaIBAGFWIhZyCUAllLJenh8BEEkQnJgbAOApwThnCQDtAyh1iJHH%2BUhAZsKtqzE2hZhFGljFsQBwDQHgMgdA2B8CMGSywfDKYhCeZozcCsWBzpuC4wiAPCmXj8bMNYUzUGHCjBcJ4b9bRo89HM1Zi4lGMxa6JAsNwIAA%3D

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:
#include <variant>
#include <iostream>

#include <boost/mp11.hpp>
using namespace boost::mp11;

template<class... T, class... A>
std::variant<T...> make_variant( std::size_t index, A&&... a )
{
    return mp_with_index<sizeof...(T)>( index, [&](auto I){

        return
 std::variant<T...>( std::in_place_index<I>, std::forward<A>( a )... );

    });
}

template<class... T>
auto init_variant( std::size_t index, std::variant<T...>& v)
{
    return mp_with_index<sizeof...(T)>( index, [&v](auto I){

        return v.emplace<I>();

    });
}

////////////////////////////////////////////////////////////////////////////////
// test

struct A
{
  void info()
  {
    std::cout << "A";
  }
};

struct B
{
  void info()
  {
    std::cout << "B";
  }
};

int main()
{
  std::size_t index = 1;

  auto v = make_variant<A, B>(index);

  std::visit([](auto&& o) { o.info(); }, v);

  auto& alt0 = init_variant(0, v); alt0.info();
  auto& alt1 = init_variant(1, v); alt1.info();
}

On Wed, Jun 30, 2021 at 3:27 PM Dominique Devienne <ddevienne_at_[hidden]>
wrote:

> Hi. I'm modernizing some code, to use std::variant instead of ad-hoc
> peudo-variant structs.
> These structs need to be encoded/decoded via avro::codec_traits
> specializations, but given
> 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.
>
> The code is below, with also a subset of the compiler errors I'm getting.
>
>
> [1]
> https://www.reddit.com/r/cpp/comments/f8cbzs/creating_stdvariant_based_on_index_at_runtime/filw8g7?utm_source=share&utm_medium=web2x&context=3
> [2]
> https://www.boost.org/doc/libs/develop/libs/mp11/doc/html/mp11.html#mp_with_indexni_f
>
> template <> struct codec_traits<std::monostate> {
> static void encode(Encoder& e, const std::monostate&) {
> e.encodeNull();
> }
>
> static void decode(Decoder& d, std::monostate&) {
> d.decodeNull();
> }
> };
>
> template <typename... Ts> struct codec_traits<std::variant<Ts...>> {
> static void encode(Encoder& e, const std::variant<Ts...>& var) {
> e.encodeUnionIndex(var.index());
> std::visit(
> [&e](const auto& alternative) {
> avro::encode(e, alternative);
> },
> var
> );
> }
>
> static void decode(Decoder& d, std::variant<Ts...>& var) {
> const size_t index = d.decodeUnionIndex();
> if (index >= sizeof...(Ts)) {
> throw avro::Exception("Invalid Union index");
> }
> auto& alternative =
> boost::mp11::mp_with_index<sizeof...(Ts)>(
> index,
> [&var](auto I) {
> return var.emplace<I>(); // I is mp_size_t<index>{}
> here
> }
> )
> ;
> avro::decode(d, alternative);
> }
> };
>
>
> 1>Messages.cpp
> 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
> 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 &'
> 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
> 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> &)'
> 1> with
> 1> [
> 1> T=pdgm::eml::etp12::Datatypes::IndexValue_item_v
> 1> ]
> 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
> 1> with
> 1> [
> 1> T=pdgm::eml::etp12::Datatypes::IndexValue_item_v
> 1> ]
> 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
> 1> with
> 1> [
> 1> T=pdgm::eml::etp12::Datatypes::IndexValue_item_v
> 1> ]
> 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
> 1> with
> 1> [
> 1> T=pdgm::eml::etp12::Datatypes::IndexValue_item_v
> 1> ]
>
>
> 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'
> 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
> 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
> 1> with
> 1> [
> 1>
> _Ty=avro::codec_traits<pdgm::eml::etp12::Datatypes::IndexValue_item_v>::decode::<lambda_7e593df011d98e42859c6ecb357b3e3f>,
> 1>
> F=avro::codec_traits<pdgm::eml::etp12::Datatypes::IndexValue_item_v>::decode::<lambda_7e593df011d98e42859c6ecb357b3e3f>
> 1> ]
> 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
> 1> with
> 1> [
> 1> T=pdgm::eml::etp12::Datatypes::IndexValue_item_v,
> 1>
> F=avro::codec_traits<pdgm::eml::etp12::Datatypes::IndexValue_item_v>::decode::<lambda_7e593df011d98e42859c6ecb357b3e3f>
> 1> ]
> 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> &)'
> 1> with
> 1> [
> 1> T=pdgm::eml::etp12::Datatypes::IndexValue_item_v
> 1> ]
> 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
> 1> with
> 1> [
> 1> T=pdgm::eml::etp12::Datatypes::IndexValue_item_v
> 1> ]
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net