Suppose that m has type map<int, string> and that we encounter a call to copy(m.begin(), m.end(), back_inserter(x)). What can we say about the type of x? What if the call were copy(x.begin(), x.end(), back_inserter(m)) instead?
I cannot get an answer. I thought x should be type map<int, string> but I tried and it's impossible because maps don't have push_back. On the other hand if I define x as a vector<int> or a vector<string> I get compilation errors.
The answer to the second question is that is doesn't matter the type of x right? it's impossible to call back_inserter(m) because maps cannot do push_back
This is a new concept for me, so it is easier if I have code to work with to figure out what is going wrong. Also any compile error messages would be helpful.
Hope that helps,
Andy
P.S. Just had another thought. It looks like the "std::copy" is on a map with two elements. So if "x" does not have two elements to copy I can see where that would be a problem. The above link shows an example using vectors. Maybe it will not work with a map?
The answer to the second question is that is doesn't matter the type of x right? it's impossible to call back_inserter(m) because maps cannot do push_back
What can we say about the type of x?
is doesn't matter the type of x right?
Sorry, Peter87, could you please explain me better your answer (or even the question itself)?
If we create an instance of a container, in this case a std::map<int, std::string> but it could be any container, don’t we need to know the type of what we put inside it before trying to put it in, no matter what method we invoke to do the job?
Even if we knew the type only at run-time, something like std::map<decltype(A), decltype(B)> mymap;
if it’s possible, and we were using the most exotic :) inserting method, aren’t we forced to guarantee the types? mymap.emplace_hint(something which returns an A, something which returns a B);
I’m afraid I could be totally misunderstanding the point here.
Enoizat, for the second question (What if the call were copy(x.begin(), x.end(), back_inserter(m)) instead?) the expression back_inserter(m) will always give an error because std::map doesn't have a push_back member function. That's why it doesn't matter what the type of x is, because no matter what we choose, it will never make this line of code work.
The types are always known at compile time. Even decltype is evaluated at compile time.