c++ - Cryptic template template parameter error -
i'm trying create function gets keys std::map
or std::unordered_map
. use simple overload, first i'd love know what's wrong code.
template<typename k, typename v, template<typename, typename> class tcontainer> std::vector<k> getkeys(const tcontainer<k, v>& mmap) { std::vector<k> result; for(const auto& itr(std::begin(mmap)); itr != std::end(mmap); ++itr) result.push_back(itr->first); return result; }
when calling std::unordered_map
, specifying all template typenames manually, clang++ 3.4 says:
template template argument has different template parameters corresponding template template parameter.
the problem std::map
, std::unordered_map
not in fact templates 2 parameters. are:
namespace std { template <class key, class t, class compare = less<key>, class allocator = allocator<pair<const key, t>>> class map; template <class key, class t, class hash = hash<key>, class pred = equal_to<key>, class allocator = allocator<pair<const key, t>>> class unordered_map; }
here's similar work:
template <typename k, typename... targs, template<typename...> class tcontainer> std::vector<k> getkeys(const tcontainer<k, targs...>& mmap) { std::vector<k> result; (auto p : mmap) result.push_back(p.first); return result; }
the version prefer:
template <typename container> auto getkeys2(const container& mmap) -> std::vector<typename container::key_type> { std::vector<typename container::key_type> result; (auto p : mmap) result.push_back(p.first); return result; }
a demo program using both functions: http://ideone.com/pckcu6
Comments
Post a Comment