Consider a free function from a third part library that expects a std::vector
as argument: void foo( std::vector<sometype>& );
Now, I write a wrapper around this type so I can add member functions. To be able to use foo()
with that type, I add an access function.
class Wrapper
{
private:
std::vector<sometype> _data;
public:
std::vector<sometype>& data() { return _data; }
const std::vector<sometype>& data() const { return _data; }
//... other stuff
};
This way, I can still use foo()
:
Wrapper a;
foo( a.data() );
But now consider another function, that expects a vector of vectors of sometype
(edit: and that adds elements into that vector) :
void bar( std::vector<std::vector<sometype>>& );
But the datatype I have is std::vector<Wrapper> vec;
Is there any way to use my wrapper type to call bar()
?
What I want to do is this:
std::vector<Wrapper> vec;
bar( ??? );
The point I want to avoid is first call bar()
with the required type, and then having to copy one by one the elements into my vector<Wrapper>
.
At first, I'd say "No", but maybe there is some smart solution ?
Edit2: to give an example, consider the following toy implementation for bar()
with an int
root datatype:
void bar( std::vector<std::vector<int>>& vv )
{
std::vector<int> v1 = { 1,2,3 };
std::vector<int> v2 = { 4,5,6 };
vv.push_back(v1);
vv.push_back(v2);
}
bar()
actually fills the vector with elements (thus the lack ofconst
). These will be later processed. Anyway, I'll check your code and try to understand it, thanks for your time ;-). Maybe you can post as an answer, even if it doesn't entirely answer the problem ? – Tadashi