Boost Fusion和Boost Qi——编译时错误

4
我无法编译以下代码:
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>

#include <string>

struct function
{
  std::string ret_type;
  std::string name;
};

BOOST_FUSION_ADAPT_STRUCT(
  function,
  (std::string, ret_type)
  (std::string, name)
)

int main() {}

在使用MSVC-11.0和boost 1.54时,我遇到了以下错误:

1>main.cpp(6084): error C3203: 'function' : unspecialized class template can't be used as a template argument for template parameter 'Sequence', expected a real type
1>main.cpp(6084): error C2955: 'boost::function' : use of class template requires template argument list
1>          e:\libs\boost_1_54_0\boost\function\function_fwd.hpp(33) : see declaration of 'boost::function'
1>main.cpp(6084): error C2913: explicit specialization; 'boost::fusion::traits::tag_of' is not a specialization of a class template
1>main.cpp(6084): error C3203: 'function' : unspecialized class template can't be used as a template argument for template parameter 'Seq', expected a real type
1>          e:\libs\boost_1_54_0\boost\function\function_fwd.hpp(33) : see declaration of 'boost::function'
1>main.cpp(6084): error C2913: explicit specialization; 'boost::fusion::extension::access::struct_member' is not a specialization of a class template
1>          e:\libs\boost_1_54_0\boost\function\function_fwd.hpp(33) : see declaration of 'boost::function'
1>main.cpp(6084): error C2913: explicit specialization; 'boost::fusion::extension::struct_member_name' is not a specialization of a class template
1>          e:\libs\boost_1_54_0\boost\function\function_fwd.hpp(33) : see declaration of 'boost::function'
1>main.cpp(6084): error C2913: explicit specialization; 'boost::fusion::extension::struct_size' is not a specialization of a class template
1>          e:\libs\boost_1_54_0\boost\function\function_fwd.hpp(33) : see declaration of 'boost::function'
1>main.cpp(6084): error C2913: explicit specialization; 'boost::fusion::extension::struct_is_view' is not a specialization of a class template
1>          e:\libs\boost_1_54_0\boost\function\function_fwd.hpp(33) : see declaration of 'boost::function'
1>main.cpp(6084): error C2913: explicit specialization; 'boost::mpl::sequence_tag' is not a specialization of a class template

如果我移除 "boost/spirit/include/qi.hpp" 的引用或者明确说明函数结构位于全局命名空间中,那么一切都没问题:

BOOST_FUSION_ADAPT_STRUCT(
  ::function,
  (std::string, ret_type)
  (std::string, name)
)
2个回答

5
当您使用BOOST_FUSION_ADAPT_STRUCT宏时,它会创建一些类型并将它们放置在boost命名空间中。这些类型的定义将引用您所给定的结构体名称。
在您的情况下,您给定的结构体名称与已经存在于boost命名空间中的名称相同:boost::function,这显然是由boost qi头文件间接包含的。由于生成的代码在boost命名空间中,它认为您意图引用boost::function而不是您的类型::function
因此,文档指出:"struct_name应该是要适配的结构体的完全命名空间限定名"。所以,在类型名称之前添加全局命名空间限定符::的解决方案是正确的。

2

在Boost Fusion的宏范围内有一个名为function的东西,它似乎比你的类型具有优先级。如果你更改你的结构体名称,则可以解决这个问题:

#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/include/qi.hpp>

#include <string>

struct my_function
{
  std::string ret_type;
  std::string name;
};

BOOST_FUSION_ADAPT_STRUCT(
  my_function,
  (std::string, ret_type)
  (std::string, name)
)

int main() {}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接