参考向量和调整大小的能力

vector of references and ability to resize

下面的代码和这个答案类似Why can't I make a vector of references?

class A 
{
public:
    A(int x) : _x(x) 
    {
        std::cout << "A : ctor\n";
    }
    
    void func() 
    {
        std::cout << "I am func \n";
    }

    int getval()
    {
        return _x;
    }

private: 
    int _x;
};

int main() 
{
    A a1(2);
    A a2(3);

    std::vector<std::reference_wrapper<A>> vec;
    vec.push_back(std::ref(a1));
    vec.push_back(std::ref(a2));
    
    for(auto v : vec)
    {
        std::remove_reference<A&>::type(v).func();      
        std::cout << std::remove_reference<A&>::type(v).getval() << "\n";
    }             
}

我需要能够调整 vec 的大小(例如 vec.resize(100))并且有类似

的东西
vec[n] = std::ref(b);

但我会收到这个我不太明白的错误:

/usr/include/c++/7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::reference_wrapper<A>; _Args = {}]’:
/usr/include/c++/7/bits/stl_uninitialized.h:527:18:   required from ‘static _ForwardIterator std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = std::reference_wrapper<A>*; _Size = long unsigned int; bool _TrivialValueType = false]’
/usr/include/c++/7/bits/stl_uninitialized.h:583:20:   required from ‘_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = std::reference_wrapper<A>*; _Size = long unsigned int]’
/usr/include/c++/7/bits/stl_uninitialized.h:645:44:   required from ‘_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = std::reference_wrapper<A>*; _Size = long unsigned int; _Tp = std::reference_wrapper<A>]’
/usr/include/c++/7/bits/vector.tcc:563:35:   required from ‘void std::vector<_Tp, _Alloc>::_M_default_append(std::vector<_Tp, _Alloc>::size_type) [with _Tp = std::reference_wrapper<A>; _Alloc = std::allocator<std::reference_wrapper<A> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/7/bits/stl_vector.h:692:21:   required from ‘void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = std::reference_wrapper<A>; _Alloc = std::allocator<std::reference_wrapper<A> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
<span class="error_line" onclick="ide.gotoLine('main.cpp',47)">main.cpp:47:19</span>:   required from here
/usr/include/c++/7/bits/stl_construct.h:75:7: error: no matching function for call to ‘std::reference_wrapper::reference_wrapper()’
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/bits/shared_ptr_base.h:56:0,
                 from /usr/include/c++/7/bits/shared_ptr.h:52,
                 from /usr/include/c++/7/memory:81,
                 from main.cpp:5:
/usr/include/c++/7/bits/refwrap.h:340:7: note: candidate: constexpr std::reference_wrapper<_Tp>::reference_wrapper(const std::reference_wrapper<_Tp>&) [with _Tp = A]
       reference_wrapper(const reference_wrapper&) = default;
       ^~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/refwrap.h:340:7: note:   candidate expects 1 argument, 0 provided
/usr/include/c++/7/bits/refwrap.h:334:7: note: candidate: std::reference_wrapper<_Tp>::reference_wrapper(_Tp&) [with _Tp = A]
       reference_wrapper(_Tp& __indata) noexcept
       ^~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/refwrap.h:334:7: note:   candidate expects 1 argument, 0 provided

有没有办法让给定的示例具有此功能?

谢谢

error: no matching function for call to ‘std::reference_wrapper::reference_wrapper()’

表示未找到包装器的默认构造函数。它没有一个,因为必须始终初始化引用。

std::vector<T>::resize 使用此默认构造函数来初始化新元素。想一想,这应该打印什么?

std::vector<std::reference_wrapper<A>> vec;
vec.resize(100); # There now must be 100 valid elements.
std::cout << vec[50].get().getval();

如果需要默认语义,请使用指针。

如果您调用调整大小作为优化,只需使用 reserve+push_back