繁体   English   中英

BOOST_FUSION_ADAPT_STRUCT的限制

[英]Limits of BOOST_FUSION_ADAPT_STRUCT

我尝试使用BOOST_FUSION_ADAPT_STRUCT宏并尝试了一些天真的东西,比如使用Fusion打印任意结构。

文档中给出的示例代码开始,我无法在我的自适应结构上执行融合序列允许的一些操作。

#include <boost/fusion/adapted.hpp>
#include <boost/fusion/sequence/io/out.hpp>
#include <boost/fusion/sequence/intrinsic.hpp>
#include <boost/fusion/view.hpp>
#include <iostream>

namespace fuz = boost::fusion;

namespace demo
{
    struct employee
    {
        std::string name;
        int age;
    };
}

// demo::employee is now a Fusion sequence
BOOST_FUSION_ADAPT_STRUCT(
    demo::employee,
    (std::string, name)
    (int, age))

int main()
{
    // tried to initialize an employee like a fusion sequence
    // but it didnt work
    // demo::employee e("bob", 42);

    demo::employee e;
    e.name = "bob";
    e.age = 42;

    // Access struct members with fusion random access functions
    // ok
    std::cout << fuz::at_c<0>(e) << std::endl; 

    // tried to print the struct like any othe fusion sequence
    // didnt work
    // std::cout << e << std::endl;

    // I made it work by using a fusion view
    // is it the right way?
    std::cout << fuz::as_nview<0, 1>(e) << std::endl;
}

这引出了以下问题:

  • 为什么Fusion magik不能在这里运行?

  • 使用视图是打印适应结构的正确方法吗?

  • 改编后的结构可以用多远作为融合序列?

来自boost::fusion文档:

I / O运算符在命名空间boost :: fusion中被重载

这意味着如果你想要隐式集成这些operator<< ,你需要在你当前的命名空间( :: here)中注入boost::fusion命名空间,或者明确地使用它们。

总结一下,添加:

using namespace boost::fusion;

应该适合你的情况。 或者为明确使用,您必须写:

boost::fusion::operator<<(std::cout, e) << std::endl;

---编辑---

稍微阅读了boost::fusion的代码之后,由于Koenigboost::fusion::operators::operator<< 的查找 ,你似乎很困惑,因为你的参数是真正的boost::fusion::sequence

这就是为什么你不需要注入boost::fusion的命名空间,也没有显式调用boost::fusion::operator<<在定义类型boost::fusion的命名空间。

一些解释:

我不会在这里解释Koenig的查找的整个概念(也称为Argument Dependent Lookup - ADL),因为这不是重点,但基本上,它表明如果你使用的是一个类型在命名空间内的变量,那么函数查找扩展到该参数的命名空间。

在这种特殊情况下,包括boost/fusion/sequence/io/out.hpp将定义boost::fusion::operator::operator<< ,然后将其注入boost::fusion命名空间。

$ cat /usr/local/include/boost/fusion/sequence/io/out.hpp
[...]
namespace boost { namespace fusion
{
    [...]
    namespace operators
    {
        template <typename Sequence>
        inline typename
            boost::enable_if<
               fusion::traits::is_sequence<Sequence>
              , std::ostream&
            >::type // this is just a SFINAE trick to ensure
                    // the function will only be selected for
                    // actual boost::fusion::sequence
        operator<<(std::ostream& os, Sequence const& seq)
        {
            return fusion::out(os, seq); // this will print out the sequence
        }
    }
    using operators::operator<<; // here the operator<< is injected
                                 // in boost::fusion
}}

这意味着使用 operator<< 的参数类型在 boost::fusion 命名空间 中的调用 将找到正确的重载。

使用类型不在此命名空间中的参数调用将无法解决operator<<的正确重载(在您的示例中就是这种情况)。

您可以通过在boost::fusion命名空间中定义类型来检查它。

namespace boost { namespace fusion {
struct employee
{
  std::string name;
  int age;
};
}}

BOOST_FUSION_ADAPT_STRUCT(
    boost::fusion::employee,
    (std::string, name)
    (int, age))

[...]
boost::fusion::employee e;
std::cout << e << std::endl; // ADL will work here

附注:如果要调试这些名称查找问题,则应使用gdb 这样,您将始终知道选择了哪个过载。 在这种情况下:

$ cat fusion.cpp
#include <iostream>
#include <cstdlib>

#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/sequence/io.hpp>

int main(int, char**)
{
  boost::fusion::vector<int, char> foo(42, '?');
  std::cout << foo << std::endl;

  return EXIT_SUCCESS;
}

$ gdb -q ./fusion
Reading symbols for shared libraries ... done
(gdb) b 10
Breakpoint 1 at 0x1000012f7: file fusion.cpp, line 10.
(gdb) r
Starting program: /Users/avallee/Projects/tmp/fusion
Reading symbols for shared libraries ++............................. done

Breakpoint 1, main (unnamed_arg=0x7fff5fbffb60, unnamed_arg=0x7fff5fbffb60) at fusion.cpp:10
10    std::cout << foo << std::endl;
(gdb) s
boost::fusion::operators::operator<< <boost::fusion::vector<int, char, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_, boost::fusion::void_> > (os=@0x7fff762b5f10, seq=@0x7fff5fbffb18) at out.hpp:38
38              return fusion::out(os, seq);

非常感谢Aurelien的详细解释。 我也在Google群组上发现了这篇帖子 正如您的解释所导致的那样,最简单的方法是将它放在演示命名空间中:

namespace demo{
    using boost::fusion::operators::operator<<;
    ...

暂无
暂无

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

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