简体   繁体   English

如何分配给struct对象的成员?

[英]How to assign to members of a struct object?

I'm taking my first steps with Boost.Hana, so please bear with me. 我正在和Boost.Hana迈出第一步,所以请耐心等待。 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). 这是我尝试将每个元组元素分配给其各自的(顺序)成员A.这不起作用(请参阅完整错误的实例 )。 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? 我在文档中读到Hana算法具有按值语义 ,但那么如何才能做到这一点呢? Is constructing an A from the hana::tuple the only thing possible? hana::tuple构建A是唯一可行的吗?

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. 要在适当的位置修改Struct ,请使用hana::accessors ,它提供hana::pair的元组,每个hana::pair都带有一个键和一个存取器函数。 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 . 此外,由于我们还没有反射,你需要使用一个像BOOST_HANA_ADAPT_STRUCT这样的宏来实现A作为hana::Struct

The other answer addresses the lambda taking an rvalue because the zipped tuple is a temporary object. 另一个答案解决了lambda采用右值,因为压缩的元组是一个临时对象。

#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; 我对Boost和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() . 我将lambda更改为[&]并将参数更改为auto&&并且我使用了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. AFAIK我不认为这是分配任何东西,但你可以使用它并从这里开始,但积极的部分是在你提供的演示代码和相同的编译器设置的同一个网站上,这样做编译没有错误。 Live Demo 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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