gpt4 book ai didi

c++ - 递归 boost::variant 类型不能用 "-std=c++11 -stdlib=libc++"编译

转载 作者:太空狗 更新时间:2023-10-29 20:28:48 25 4
gpt4 key购买 nike

考虑以下代码:

#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.

这是怎么回事?为什么我不能用指定的选项编译它?有什么办法可以解决吗?

最佳答案

boost::recursive_wrapper<foo>是处理不完整类型的工具,同时仍然保持例如的错觉。访问者认为一个变体真的持有foo .

关于c++ - 递归 boost::variant 类型不能用 "-std=c++11 -stdlib=libc++"编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12136848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com