3

考虑以下代码:

#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 安装了 boost 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.

这里发生了什么?为什么我不能用指定的选项编译它?有没有办法解决这个问题?

4

2 回答 2

5

boost::recursive_wrapper<foo>是处理不完整类型的工具,同时仍然保持例如访问者的错觉,即变体确实持有foo.

于 2012-08-27T16:39:58.117 回答
1

您不能以递归方式使用不完整类型作为模板参数。编译器错误消息非常清楚。

于 2012-08-27T07:51:57.780 回答