Lau*_*ent 8 c++ metaprogramming boost-fusion
我试图玩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;
}
Run Code Online (Sandbox Code Playgroud)
这引出了以下问题:
为什么Fusion magik不能在这里运行?
使用视图是打印适应结构的正确方法吗?
改编后的结构可以用多远作为融合序列?
从boost::fusion文档:
I/O运算符在命名空间boost :: fusion中被重载
这意味着如果您想要隐式集成这些operator<<,您需要boost::fusion在当前命名空间(::此处)中注入命名空间,或者明确使用它们.
总结一下,添加:
using namespace boost::fusion;
Run Code Online (Sandbox Code Playgroud)
应该适合你的情况.或者为明确使用,您必须写:
boost::fusion::operator<<(std::cout, e) << std::endl;
Run Code Online (Sandbox Code Playgroud)
---编辑---
boost::fusion稍微读了一下代码后,似乎你很困惑,因为如果你的论证是真实的,那么选择Koenig的查找.boost::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
}}
Run Code Online (Sandbox Code Playgroud)
这意味着使用 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
Run Code Online (Sandbox Code Playgroud)
附注:如果要调试这些名称查找问题,则应使用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);
Run Code Online (Sandbox Code Playgroud)
非常感谢 Aurelien 的详细解释。我也在谷歌群组上找到了这篇文章。正如您的解释所导致的,使事情正常工作的最简单方法是将其放在 demo 命名空间中:
namespace demo{
using boost::fusion::operators::operator<<;
...
Run Code Online (Sandbox Code Playgroud)