使用包含不完整类型的`std :: vector`递归定义和访问`boost :: variant` - libstdc ++ vs libc ++

Vit*_*meo 6 c++ boost boost-variant libc++ c++17

我试图boost::variant使用不完整的包装类和std::vector我的间接技术来定义和访问"递归" .我的实现适用于libstdc ++,但不适用于libc ++.


这是我定义我的变体的方式:

struct my_variant_wrapper;

using my_variant_array = std::vector<my_variant_wrapper>; // <- indirection here
using my_variant = boost::variant<int, my_variant_array>;

struct my_variant_wrapper
{
    my_variant _v;

    template <typename... Ts>
    my_variant_wrapper(Ts&&... xs) : _v(std::forward<Ts>(xs)...) { }
};
Run Code Online (Sandbox Code Playgroud)

std::vector用来引入间接(因此动态分配将阻止my_variant具有无限大小).

由于纸张N4510 ("标准容器的最小不完全类型支持")std::vector<my_variant_wrapper>,我非常有信心我可以使用,哪里my_variant_wrapper不完整的类型:


我随后访问该变体如下:

struct my_visitor
{
    void operator()(int x) const { }
    void operator()(const my_variant_array& arr) const
    {
        for(const auto& x : arr)            
            boost::apply_visitor(*this, x._v);            
    }
};

int main()
{
    my_variant v0 = my_variant_array{
        my_variant{1}, my_variant{2}, my_variant_array{
            my_variant{3}, my_variant{4}
        }
    };

    boost::apply_visitor(my_visitor{}, v0);
}
Run Code Online (Sandbox Code Playgroud)

coliru上提供了一个最小的完整示例.


  • 我正在使用以下标志:

    -std = c ++ 1z -Wall -Wextra -Wpedantic

  • BOOST_VERSION评估为106100.

代码:

  • 按预期编译和运行:

    • g ++ (测试版本:6.1和7),libstdc ++.

    • clang ++ (测试版本:3.8),libstdc ++.

    • (作为奖励,它也适用于std::variant进行适当的更改!)

  • 无法编译:

    • clang ++ (测试版本:3.8,4),带有libc ++.

这是我在使用libc ++编译clang ++时遇到的错误:

In file included from prog.cc:2:
In file included from /usr/local/boost-1.61.0/include/boost/variant.hpp:17:
/usr/local/boost-1.61.0/include/boost/variant/variant.hpp:1537:28: error: no matching member function for call to 'initialize'
              initializer::initialize(
              ~~~~~~~~~~~~~^~~~~~~~~~
/usr/local/boost-1.61.0/include/boost/variant/variant.hpp:1692:9: note: in instantiation of function template specialization 'boost::variant<int, std::__1::vector<my_variant_wrapper, std::__1::allocator<my_variant_wrapper> > >::convert_construct<my_variant_wrapper>' requested here
        convert_construct(operand, 1L);
        ^
prog.cc:15:38: note: in instantiation of function template specialization 'boost::variant<int, std::__1::vector<my_variant_wrapper, std::__1::allocator<my_variant_wrapper> > >::variant<my_variant_wrapper>' requested here
    my_variant_wrapper(Ts&&... xs) : _v(std::forward<Ts>(xs)...) { }
                                     ^
/usr/local/libcxx-head/include/c++/v1/memory:1783:31: note: in instantiation of function template specialization 'my_variant_wrapper::my_variant_wrapper<my_variant_wrapper &>' requested here
            ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
                              ^
/usr/local/libcxx-head/include/c++/v1/memory:1694:18: note: in instantiation of function template specialization 'std::__1::allocator<my_variant_wrapper>::construct<my_variant_wrapper, my_variant_wrapper &>' requested here
            {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
                 ^

...
Run Code Online (Sandbox Code Playgroud)

wandbox上提供了完整错误.


为什么代码没有用libc ++编译? (这可能是libc ++的N4510实现中需要报告的缺陷吗?)

该错误似乎表明该变体未能检测到应该初始化哪些成员,但实际上我无法理解它.我也对使用libstdc ++ (具有相同的boost版本)按预期工作的事实感到困惑.

T.C*_*.C. 7

我在回溯中看到了这个:

注意:在my_variant_wrapper::my_variant_wrapper<my_variant_wrapper &>此处请求的函数模板特化的实例化

这清楚地表明您的构造函数模板正在劫持复制构造函数.

限制它,你的问题就消失了.


实现之间的区别是由于vector复制构造函数复制元素的方式.libstdc ++将源元素视为const:

vector(const vector& __x)
  : _Base(__x.size(),
    _Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()))
{
    this->_M_impl._M_finish =
    std::__uninitialized_copy_a(__x.begin(), __x.end(),
                                this->_M_impl._M_start,
                                _M_get_Tp_allocator());
}
Run Code Online (Sandbox Code Playgroud)

因为begin()并且end()被调用const vector& x,它们返回常量迭代器.

libc ++将源元素视为非元素const:

template <class _Tp, class _Allocator>
vector<_Tp, _Allocator>::vector(const vector& __x)
    : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc()))
{
#if _LIBCPP_DEBUG_LEVEL >= 2
    __get_db()->__insert_c(this);
#endif
    size_type __n = __x.size();
    if (__n > 0)
    {
        allocate(__n);
        __construct_at_end(__x.__begin_, __x.__end_, __n);
    }
}
Run Code Online (Sandbox Code Playgroud)

__begin_并且__end_pointers,并且因为const很浅,所以const- __x不会成为指针const.

两者都符合要求,因为CopyInsertable需要来自两者const和非const来源的可复制性.但是,您的模板仅劫持从非复制const(因为它丢失了const模板/非模板仲裁器的大小写复制),因此您只能在libc ++中看到问题.