Reputation: 971
I would like to transform from foo to bar.
struct A
{
explicit A(int d): m_d(d) {};
private:
int m_d;
};
A some_function (int i) { return A(2*i); }
std::vector<int> foo;
std::vector<A> bar; // This is part of a Library not under my control
The interface only provides pbar
std::vector<A>* pbar =&bar; // This is the interface to the part of a Library not under my control
Is it legal to dereference pbar und use it as argument for back_inserter? Why?
std::transform (foo.begin(), foo.end(), std::back_inserter(*pbar), some_function);
A full example is here : http://coliru.stacked-crooked.com/a/2aec8d000cabf78b
Upvotes: 1
Views: 1636
Reputation: 11696
Yes, there's no problem with doing that at all. I'm not really sure what the source of your confusion is, so I'm not sure how to answer "why." If you have a non-const
pointer to an object, you're allowed to dereference it and pass it to functions that need a non-const
reference (like std::back_inserter
).
Upvotes: 2