简体   繁体   中英

How to assign to members of a struct object?

I'm taking my first steps with Boost.Hana, so please bear with me. I have

#include <boost/hana.hpp>
namespace hana = boost::hana;
using namespace hana::literals;

#include <string>

struct A
{
  int integer;
  std::string string;
};

int main()
{
  auto tuple = hana::make_tuple(42, "42");
  A a;
  hana::for_each(hana::zip(hana::members(a), tuple), [](auto& element) { element[0_c] = element[1_c]; });
}

This is my attempt at assigning each tuple element to its respective (sequential) member of A. This does not work (see live example for complete error). It boils down to

main.cpp:19:54: note: candidate function [with $0 = boost::hana::tuple<int, int>] not viable: expects an l-value for 1st argument

 hana::for_each(hana::zip(hana::members(a), input), [](auto& element) { element[0_c] = element[1_c]; });
                                                    ^

I read in the documentation that Hana algorithms have by-value semantics , but then how would one go about doing this kind of thing? Is constructing an A from the hana::tuple the only thing possible?

To modify a Struct in place, use hana::accessors which provides a tuple of hana::pair s each with a key and an accessor function. Also since we don't have reflection yet you need to use one of the macros like BOOST_HANA_ADAPT_STRUCT to implement A as a hana::Struct .

The other answer addresses the lambda taking an rvalue because the zipped tuple is a temporary object.

#include <cassert>
#include <boost/hana.hpp>
namespace hana = boost::hana;
using namespace hana::literals;

#include <string>

struct A
{
  int integer;
  std::string string;
};
BOOST_HANA_ADAPT_STRUCT(A, integer, string);

int main()
{
  auto tuple = hana::make_tuple(42, "42");
  A a;
  hana::for_each(
    hana::zip(hana::accessors<A>(), tuple),
    [&a](auto&& element) {
      auto accessor_pair = hana::at_c<0>(element);
      auto get_member = hana::second(accessor_pair);
      get_member(a) = hana::at_c<1>(element);
    });

  assert(a.integer == 42 && a.string == "42");
}

I'm not real familiar with Boost nor Hana; but I went to their website and read a little bit of their documentation on some of their objects, functions etc. I do not know if this will help you but I was able to modify your code slightly and I got this to compile:

int main() {
    A a;
    auto tuple = hana::make_basic_tuple( 42, "42" );

    hana::for_each( hana::zip( hana::members(a), tuple), 
                               [&](auto&& element) {
                                   hana::at( tuple, hana::size_c<0> );
                                   hana::at( tuple, hana::size_c<1> );
                               }
                  );

}

I changed your lambda to have [&] and changed the parameter to be an auto&& and I used hana::at() . AFAIK I do not think this is assigning anything at the moment but you might be able to use this and go from here, but the positive part is that on the same website your provided with your demo code and with the same compiler settings, this did compile without error. Live Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM