2014-02-06 53 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() {}