2

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 =)

1
  • reference_wrappers have to be initialized with what to refer to. Commented Apr 16, 2014 at 14:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I replaced boost::array with std::vector and it to works now. It now creates the references as it pushes them into the vector. Maybe this is another question, but I wanted to know your opinion. I'm not quite sure if this is better or worse, comparing it to your approach. What represents more overhead, the reserved vector or the smart_ptr?
I tried to answer as a comment, but it got too long. I'll edit my answer to add a response.
Thank you very much. For now, I'll go with what I think it's more readable. If performance suffers, I, uh... well, I'll think of something if and when that happens.
An excellent approach! Make it correct, make it readable, THEN if performance is an issue, profile it and hand-optimize it.
1

boost::arrays 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.