Search code examples
c++templatesboosttemplate-meta-programmingboost-fusion

returning column from a std vector of fusion vectors, without copying


I have a "table" represented by a std::vector where every element is a boost::fusion::vector. I need to return a representation of a "column" of this table as a std::vector, without copying any values over. What is the best way of doing this? I am trying to construct a std::vector of nviews where n is the column number, but it does not seem to be working. Am I on the right track or are there other ways of going about this?


Solution

  • As per the docs here, the at<N>(s) method of the boost::fusion::vector's return a reference to the contained element.

    Also if you are using boost library version 1.48, they support the move semantics of C++11. What this means is you can have a function which loops over the size of your table for a particular column no, say i, and populates a column vector which is an std::vector. You then just return this column vector and the calling function would get the moved std::vector. No copies are made at any point.

    You can read more about the move semantics here at the standards page itself. You can also watch the video of Bjarne Stroustrup explaining it in brief here. Jump to minute 37 and watch till minute 43.