I'm trying to learn the currently accepted features of c++0x and I'm having trouble with auto and decltype. As a learning exercise I'm extending the std class list with some generic functions.
template<class _Ty, class _Ax = allocator<_Ty>>
class FList : public std::list<_Ty, _Ax>
{
public:
void iter(const function<void (_Ty)>& f)
{
for_each(begin(), end(), f);
}
auto map(const function<float (_Ty)>& f) -> FList<float>*
{
auto temp = new FList<float>();
for (auto i = begin(); i != end(); i++)
temp->push_back(f(*i));
return temp;
}
};
auto *ints = new FList<int>();
ints->push_back(2);
ints->iter([](int i) { cout << i; });
auto *floats = ints->map([](int i) { return (float)i; });
floats->iter([](float i) { cout << i; });
For the member map I want the return type to be generic depending on what the passed function returns. So for the return type I could do something like this.
auto map(const function<float (_Ty)>& f) -> FList<decltype(f(_Ty))>*
This would also need to remove the float type in the function template.
auto map(const function<auto (_Ty)>& f) -> FList<decltype(f(_Ty))>*
I could use a template class but that makes the use of instances more verbose since i have to specify the return type.
template<class T> FList<T>* map(const function<T (_Ty)>& f)
To sum of my question i'm trying to figure out how to define map without using a template class and still have it generic in the type it returns.