繁体   English   中英

递归boost :: variant类型不能使用“-std = c ++ 11 -stdlib = libc ++”进行编译

[英]Recursive boost::variant type doesn't compile with “-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;
}

使用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.

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

boost::recursive_wrapper<foo>是处理不完整类型的工具,同时仍然保持游客的错觉,即变体真正拥有foo

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM