2012-08-27 51 views
3

考慮下面的代碼編譯:遞歸的boost ::變種類型不與 「-std = C++ 11 -stdlib =的libC++」

#include <vector> 
#include <boost/variant.hpp> 

struct foo; 

typedef boost::variant<foo> bar; 

struct foo 
{ 
    std::vector<bar> baz; 
}; 

int main() 
{ 
    foo f; 
    return 0; 
} 

大廈在Mac OS X和Xcode 4.4(我有通過自制軟件安裝1.50.0):

  • clang++ test.cc:沒有錯誤。
  • clang++ -stdlib=libc++ test.cc:沒有錯誤。
  • clang++ -std=c++11 test.cc:沒有錯誤。
  • clang++ -std=c++11 -stdlib=libc++ test.cc很多錯誤!

 

/usr/local/include/boost/type_traits/has_nothrow_constructor.hpp:24:40: error: incomplete type 'foo' used in type trait expression 
    BOOST_STATIC_CONSTANT(bool, value = BOOST_HAS_NOTHROW_CONSTRUCTOR(T)); 
            ^
...snip... 
test.cc:10:19: note: in instantiation of template class '...snip...' requested here 
     std::vector<bar> baz; 
         ^
test.cc:8:8: note: definition of 'foo' is not complete until the closing '}' 
struct foo 
    ^

/usr/local/include/boost/mpl/next_prior.hpp:31:22: error: type 'int' cannot be used prior to '::' because it has no members 
    typedef typename T::next type; 
        ^
test.cc:10:19: note: in instantiation of template class '...snip...' requested here 
     std::vector<bar> baz; 
         ^

/usr/local/include/boost/mpl/sizeof.hpp:27:20: error: invalid application of 'sizeof' to an incomplete type 'foo' 
    : mpl::size_t< sizeof(T) > 
        ^~~~~~~~~ 

test.cc:10:19: note: in instantiation of template class '...snip...' requested here 
     std::vector<bar> baz; 
         ^
test.cc:8:8: note: definition of 'foo' is not complete until the closing '}' 
struct foo 
    ^

...big snip... 

8 errors generated. 

這是怎麼回事嗎?爲什麼我不能使用指定的選項編譯它?有什麼辦法可以解決這個問題嗎?

+0

我認爲不允許在'typedef'語句中使用未定義類型('foo')。 – Walter

+2

你可以嘗試'typedef boost :: variant > bar;'。 –

+0

@LucDanton謝謝,那就是解決方案!發佈一個答案,我會接受它。 – jtbandes

回答

5

boost::recursive_wrapper<foo>是處理不完整類型的工具,同時仍然保持對例如幻燈片的錯覺。訪問者說一個變體真的擁有foo

1

您不能以遞歸的方式使用不完整的類型作爲模板參數。編譯器錯誤信息非常清晰。