I had to build a const_iterator wrapper to exchange generic programming with dynamic binding (never mind this). However std::set<Base*>::const_iterator::operator*() gives me something I didn’t expect, because I have trouble returning it in a Derived* const& where Derived publicly inherits from Base.
template <typename T, typename Container> class StdConstIterator : public ConstIteratable<T> {
private:
typename Container::const_iterator it;
public:
T const& operator*() const {
return *it; // g++ says: warning: returning reference to temporary
}
};
// invokation
StdConstIterator<Derived*,std::set<Base*> > si;
While I see that it might be reasonable for pointers to copy them instead of returning a reference, I fail to find a specialisation in my STL’s implementation. Could you shed some light on the issue, please?
Note: You might know the routine; Unfortunately no C++11 support, so I cannot decltype myself out of this. But this is more of a “what the heck is going on here?” sort of question, anyway.
Most likely is that there’s some conversion going on. Instead of
Tyou should use the actual iterator’s value type,typename Container::const_iterator::value_type. If that type isn’t the same asT, then the conversion creates a temporary, to which you attempt to return a reference.On the other hand, if you do want the conversion, then return by value,
T operator*() const.