请考虑以下代码:
#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;
}
使用Xcode 4.4在Mac OS X上构建(我通过Homebrew安装了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 :(得分:5)
boost::recursive_wrapper<foo>
是处理不完整类型的工具,同时仍然保留了不完整类型的错觉。访问者认为变体确实包含foo
。
答案 1 :(得分:1)
您不能以递归方式将不完整类型用作模板参数。编译器错误消息非常清楚。