2014-02-06 54 views
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升压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) 
) 

回答

5

当您使用BOOST_FUSION_ADAPT_STRUCT宏,它会创建某些类型,并将它们放置在boost命名空间中。这些类型的定义将引用您给它的结构名称。

在你的情况下,你给了一个名字,它和boost名字空间中已经存在的名字相同:boost::function,这显然间接地包含在boost qi头文件中。由于生成的代码位于boost名称空间中,因此它认为您的意思是指boost::function而不是::function

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

2

增强融合的宏观范围内有一些叫做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() {}