Reputation: 2049
I have a class with a boost::array
type that stores references to std::vector
.
I want to construct an array of references (I'm trying to DI, or so I think), but haven't succeeded so far.
class RefHolder
{
public:
boost::array<boost::reference_wrapper<std::vector<int> >, 3> sumsArray;
};
class OriginalHolder
{
public:
OriginalHolder(boost::array<boost::reference_wrapper<std::vector<int> >, 3> & pSumsArray)
{
sumsArray[0] = boost::ref(_sums);
}
private:
std::vector<int> _sums;
}
int main()
{
RefHolder ref;
OriginalHolder original(ref.sumsArray);
return 0;
}
I don't really get what the compiler is telling me. Does array
even work with reference_wrapper
?
C:/ide-4.6-workspace/chunkybacon/boost/boost/array.hpp:60: error: no matching function for call to 'boost::reference_wrapper<std::vector<int, std::allocator<int> > >::reference_wrapper()'
C:/ide-4.6-workspace/chunkybacon/boost/boost/ref.hpp:43: note: candidates are: boost::reference_wrapper<T>::reference_wrapper(T&) [with T = std::vector<int, std::allocator<int> >]
C:/ide-4.6-workspace/chunkybacon/boost/boost/ref.hpp:33: note: boost::reference_wrapper<std::vector<int, std::allocator<int> > >::reference_wrapper(const boost::reference_wrapper<std::vector<int, std::allocator<int> > >&)
C:/ide-4.6-workspace/chunkybacon/TEST/TEST.cc: In constructor 'RefHolder::RefHolder()':
C:/ide-4.6-workspace/chunkybacon/TEST/TEST.cc:6: note: synthesized method 'boost::array<boost::reference_wrapper<std::vector<int, std::allocator<int> > >, 3u>::array()' first required here
C:/ide-4.6-workspace/chunkybacon/TEST/TEST.cc: In function 'int main()':
C:/ide-4.6-workspace/chunkybacon/TEST/TEST.cc:32: note: synthesized method 'RefHolder::RefHolder()' first required here
cc: C:/QNX641/host/win32/x86/usr/lib/gcc/i386-pc-nto-qnx6.4.0/4.3.3/cc1plus caught signal 1
Of course I could just use plain pointers, and it works, but I still have the doubt and, if I can fix it, I'd rather use references.
I'm working with QNX 6.4.1 and GCC 4.3.3.
Thank you in advance for any help =)
Upvotes: 2
Views: 293
Reputation: 9434
When you initially create the array you have "empty" entries. A reference cannot really be empty (OK, it could be, but it's a very bad idea), so references do not work as array entries.
Consider using boost::smart_ptr<vectorint> >
instead. I realize that's not exactly what you want (pointer vs reference) but it works which is a big point in its favor.
------------Response to question in the comments---------
The difference is that a vector of references must be referencing objects that exist somewhere else -- somewhere that manages the lifetime of these objects and ensures that they live longer than the references to them do.
A vector of smart pointers must be pointing to heap-allocated objects, and manages the lifetime of those objects.
Both of these approaches have valid uses.
Upvotes: 1
Reputation: 7775
boost::array
s constructor is trying to initialize all of its members with the default constructor of the defined type, which in this case is a boost::reference_wrapper
, which has no default constructor, because references can not be undefined.
Upvotes: 1