考慮下面的代碼編譯:遞歸的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.
這是怎麼回事嗎?爲什麼我不能使用指定的選項編譯它?有什麼辦法可以解決這個問題嗎?
我認爲不允許在'typedef'語句中使用未定義類型('foo')。 – Walter
你可以嘗試'typedef boost :: variant> bar;'。 –
@LucDanton謝謝,那就是解決方案!發佈一個答案,我會接受它。 – jtbandes