【问题标题】:Circular dependency and std::vector::insert循环依赖和 std::vector::insert
【发布时间】:2017-04-22 03:48:27
【问题描述】:

我有这个文件:

啊.h

struct B;

struct A
{
    A(... B &b) :
        b(b) {}

    B &b;
};

还有B.h:

#include "A.h"
struct B{
...
std::vector<A> as;
}

还有这段代码:

std::vector<A> manya;
std::vector<B> bs;
//fill bs
for(size_t i=0; i<bs.size(); i++)
  manya.insert(manya.end(), bs[i].as.begin(), bs[i].as.end());

但是,我收到此错误(A=FindAffineShapeArgs 和 B=Wrapper):

/usr/include/c++/5/bits/stl_algobase.h(564): error: function "FindAffineShapeArgs::operator=(const FindAffineShapeArgs &)" (declared implicitly) cannot be referenced -- it is a deleted function
        *--__result = std::move(*--__last);
                    ^
          detected during:
            instantiation of "_BI2 std::__copy_move_backward<true, false, std::random_access_iterator_tag>::__copy_move_b(_BI1, _BI1, _BI2) [with _BI1=FindAffineShapeArgs *, _BI2=FindAffineShapeArgs *]" at line 606
            instantiation of "_BI2 std::__copy_move_backward_a<_IsMove,_BI1,_BI2>(_BI1, _BI1, _BI2) [with _IsMove=true, _BI1=FindAffineShapeArgs *, _BI2=FindAffineShapeArgs *]" at line 615
            instantiation of "_BI2 std::__copy_move_backward_a2<_IsMove,_BI1,_BI2>(_BI1, _BI1, _BI2) [with _IsMove=true, _BI1=FindAffineShapeArgs *, _BI2=FindAffineShapeArgs *]" at line 686
            instantiation of "_BI2 std::move_backward(_BI1, _BI1, _BI2) [with _BI1=FindAffineShapeArgs *, _BI2=FindAffineShapeArgs *]" at line 636 of "/usr/include/c++/5/bits/vector.tcc"
            instantiation of "void std::vector<_Tp, _Alloc>::_M_range_insert(std::vector<_Tp, _Alloc>::iterator, _ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _Tp=FindAffineShapeArgs, _Alloc=std::allocator<FindAffineShapeArgs>, _ForwardIterator=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>]" at line 1377 of "/usr/include/c++/5/bits/stl_vector.h"
            instantiation of "void std::vector<_Tp, _Alloc>::_M_insert_dispatch(std::vector<_Tp, _Alloc>::iterator, _InputIterator, _InputIterator, std::__false_type) [with _Tp=FindAffineShapeArgs, _Alloc=std::allocator<FindAffineShapeArgs>, _InputIterator=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>]" at line 1100 of "/usr/include/c++/5/bits/stl_vector.h"
            instantiation of "std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::const_iterator, _InputIterator, _InputIterator) [with _Tp=FindAffineShapeArgs, _Alloc=std::allocator<FindAffineShapeArgs>, _InputIterator=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>, <unnamed>=void]" at line 382 of
                      "/home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Descriptors/hesaff/pyramid.cpp"

/usr/include/c++/5/bits/stl_algobase.h(340): error: function "FindAffineShapeArgs::operator=(const FindAffineShapeArgs &)" (declared implicitly) cannot be referenced -- it is a deleted function
          *__result = *__first;
                    ^
          detected during:
            instantiation of "_OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II=FindAffineShapeArgs *, _OI=FindAffineShapeArgs *]" at line 402
            instantiation of "_OI std::__copy_move_a<_IsMove,_II,_OI>(_II, _II, _OI) [with _IsMove=false, _II=FindAffineShapeArgs *, _OI=FindAffineShapeArgs *]" at line 440
            instantiation of "_OI std::__copy_move_a2<_IsMove,_II,_OI>(_II, _II, _OI) [with _IsMove=false, _II=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>, _OI=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>]" at line 472
            instantiation of "_OI std::copy(_II, _II, _OI) [with _II=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>, _OI=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>]" at line 637 of "/usr/include/c++/5/bits/vector.tcc"
            instantiation of "void std::vector<_Tp, _Alloc>::_M_range_insert(std::vector<_Tp, _Alloc>::iterator, _ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _Tp=FindAffineShapeArgs, _Alloc=std::allocator<FindAffineShapeArgs>, _ForwardIterator=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>]" at line 1377 of "/usr/include/c++/5/bits/stl_vector.h"
            instantiation of "void std::vector<_Tp, _Alloc>::_M_insert_dispatch(std::vector<_Tp, _Alloc>::iterator, _InputIterator, _InputIterator, std::__false_type) [with _Tp=FindAffineShapeArgs, _Alloc=std::allocator<FindAffineShapeArgs>, _InputIterator=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>]" at line 1100 of "/usr/include/c++/5/bits/stl_vector.h"
            instantiation of "std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::const_iterator, _InputIterator, _InputIterator) [with _Tp=FindAffineShapeArgs, _Alloc=std::allocator<FindAffineShapeArgs>, _InputIterator=__gnu_cxx::__normal_iterator<FindAffineShapeArgs *, std::vector<FindAffineShapeArgs, std::allocator<FindAffineShapeArgs>>>, <unnamed>=void]" at line 382 of
                      "/home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Descriptors/hesaff/pyramid.cpp"

compilation aborted for /home/luca/Dropbox/HKUST/CloudCache/cloudcache/CloudCache/Descriptors/hesaff/pyramid.cpp (code 2)

为什么会这样?

这可能与this 有关,但情况不同

【问题讨论】:

  • 它与循环依赖或向量(甚至 omp)无关。错误提到operator=。尝试写一个minimal reproducible example,你会看到发生了什么。

标签: c++ struct include circular-dependency


【解决方案1】:

问题是你的结构A 有一个引用成员B&amp; b。由于无法重新分配引用,因此编译器无法为您隐式生成复制或移动分配运算符。

但是,对于 std::vector&lt;T&gt;::insert 的这种特殊重载,标准 ([sequence.reqmts]/4) 声明

要求:T 应该是 EmplaceConstructible 从 *i 到 X。对于 vector 和 deque,T 还应可 MoveInsertable 到 X、MoveConstructible、MoveAssignable 和可交换 (17.6.3.2)。 [...]

这就是编译器抱怨隐式删除的赋值运算符的原因。

如果您真的希望它成为引用成员,您可以在结构 A 中使用 std::reference_wrapper&lt;B&gt; 而不是 B&amp;。

【讨论】:

  • 我不能使用std::reference_wrapper&lt;B&gt;,因为我得到了一个不完整的结构错误
  • @justHelloWorld 您使用的是哪个编译器?它适用于 gcc 6.3 和 clang 4.0,请参阅 here。
  • @justHelloWorld 你是不是忘了#include &lt;functional&gt;?
  • @justHelloWorld A std::reference_wrapper 只是一个像引用一样的类(它隐式转换为T&amp;,可以从T&amp; 构造,如果T 是一个函数类型,你甚至可以像通常对函数对象的引用一样调用它),但也实现了赋值,使其表现得像一个常规类型。它的设计目的是让您可以将对对象的引用放在通常不支持存储引用的容器中。最好阅读我上面链接的参考资料。
  • @justHelloWorld 然后创建一个std::reference_wrapper&lt;B const&gt; 并使用std::cref 而不是std::ref 在A 的构造函数中从B&amp;/B const&amp; 创建引用包装器。
猜你喜欢
  • 1970-01-01
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多