【问题标题】:Recursive boost::variant type doesn't compile with "-std=c++11 -stdlib=libc++"递归 boost::variant 类型不能用“-std=c++11 -stdlib=libc++”编译
【发布时间】:2012-08-21 15:18:53
【问题描述】:

考虑以下代码:

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

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

【问题讨论】:

  • 我认为在typedef语句中使用未定义类型(foo)是不允许的。
  • 你可以试试typedef boost::variant&lt;boost::recursive_wrapper&lt;foo&gt;&gt; bar;
  • @LucDanton 谢谢,这就是解决方案!发布答案,我会接受。

标签: c++ boost c++11 compiler-errors clang


【解决方案1】:

boost::recursive_wrapper&lt;foo&gt; 是处理不完整类型的工具,同时仍然保持例如错觉。变体真正持有foo的访问者。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2017-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多