提升融合和提升qi - 编译时错误

时间:2014-02-06 13:20:26

标签: c++ boost

我无法编译以下代码:

#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() {}
带有boost 1.54的MSVC-11.0给出了以下错误:

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 个答案:

答案 0 :(得分:5)

当您使用BOOST_FUSION_ADAPT_STRUCT宏时,它会创建一些类型并将它们放在boost命名空间内。这些类型的定义将引用您提供的结构名称。

在您的情况下,您为结构提供了一个名称,该名称与boost命名空间中已存在的名称相同:boost::function,这显然是由boost qi标头间接包含的。由于生成的代码位于boost命名空间中,因此它认为您的意思是引用boost::function而不是您的类型::function

出于这个原因,documentation说:“struct_name应该是要调整的结构的完全命名空间限定名称”。因此,在类型名称之前添加全局命名空间限定符::的解决方案是正确的。

答案 1 :(得分:2)

boost boost的宏范围内有一些叫做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() {}