I have a function with a signature
void Foo(list<const A*>)
and I want to pass it a
list<A*>
How do I do this?
(plz note – the list isn’t constant, only the member of the list)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem you have is that even though
T *can be implicitly converted to aT const *, the template system isn’t “aware” of that, so awhatever<T *>andwhatever<T const *>are completely unrelated types, and there’s no implicit conversion from one to the other.To avoid the problem, I’d probably avoid passing a collection at all. Instead I’d have the function take a pair of iterators. A
list<A *>::iteratorcan be implicitly converted to alist<A *>::const_iterator. For that matter, I’d probably make the function a template, so it can take iterators of arbitrary type.This is likely to save you quite a bit of trouble — a
listis only rarely a good choice of container, so there’s a very large chance that someday you’ll want to change fromlist<A *>tovector<A *>or perhapsdeque<A *>— and if you make your function generic, you’ll be able to do that without rewriting the function at all.