Created
January 29, 2020 13:15
-
-
Save bradphelan/da494160adb32138b46aba4ed3fff967 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "boost/type_traits/declval.hpp" | |
#include "boost/foreach.hpp" | |
#include <vector> | |
template <int N, typename T> | |
struct NVector{ | |
typedef std::vector<typename NVector<N-1,T>::type> type; | |
}; | |
template <typename T> struct NVector<1,T> { | |
typedef std::vector<T> type; | |
}; | |
template <typename T, typename Mapper> | |
struct MapResult { | |
typedef decltype(boost::declval<Mapper>()(boost::declval<T>())) type; | |
}; | |
template <typename T, typename Mapper> | |
struct MapResult<std::vector<T>, Mapper> { | |
typedef std::vector<typename MapResult<T, Mapper>::type> type; | |
}; | |
template <typename T, typename Mapper> | |
typename MapResult<T, Mapper>::type Map(T const& elem, Mapper&& mapper) | |
{ | |
return mapper(elem); | |
} | |
template <typename T, typename Mapper> | |
typename MapResult<std::vector<T>, Mapper>::type Map(std::vector<T> const& vector, Mapper&& mapper) | |
{ | |
MapResult<std::vector<T>, Mapper>::type out; | |
out.reserve(vector.size()); | |
BOOST_FOREACH(auto& v , vector) | |
out.push_back(Map(v, mapper)); | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment