
Zitat von John Dlugosz
A co-worker mentioned an interesting issue. I thought some metaprogramming, or at least more advanced insight into STL, might apply here.
Consider a class derived from an STL collection. It features a special find(x) function that works with his search criteria. It returns an iterator into the collection. There are two forms needed: iterator find(x); const_iterator find(x) const; and the annoying part is that they contain exactly the same code. The implementation uses the inherited iterators and collection features to do the finding.
The larger question is, is there a better way to approach that?
The immediate question is, my thought of using a template helper requires the return type to be determined based on the constness of the incoming parameter for the collection type.
the return type cannot be parametrized based on the const-ness of "this". but parametrizing _functions_ based on constness rarely makes sense anyway, the best aproach imho is to keep the overloads but let them only handle the const/non-const specific stuff and forward the rest. in your example: private: node_ptr find_node(x) const; public: iterator find(x){ return iterator(find_node(x)); } const_iterator find(x) const{ return const_iterator(find_node(x)); }