Topic: Overloading the cast operators
Author: jgottman@carolina.rr.com ("Joe Gottman")
Date: Sat, 16 Jul 2005 00:36:43 GMT Raw View
Has anyone thought about changing the language to allow overloading of the
cast operators? The new tr1::shared_ptr class defines three pseudo-cast
operators, static_pointer_cast, const_pointer_cast, and
dynamic_pointer_cast, it would be much easier to write generic code if these
were spelled static_cast, const_cast and dynamic_cast. Also, it's always a
hassle to try to convert an iterator to a const_iterator. A const_cast
would make this much easier. This would be useful if you wanted to write
two versions of a function, one that takes a const reference as a parameter
and returns a const_iterator, and one that takes a non-const reference as a
parameter and returns an iterator.
vector<int>::const_iterator get_iterator(const vector<int> &vec);
vector<int>::iterator get_iterator(vector<int> &vec)
{
const vector<int> &const_vec = vec;
return const_cast<vector<int>::iterator>(get_iterator(const_vec));
}
Joe Gottman
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]