[英]Define a std::map with a templated Key type based on a function that is received as a parameter
My goal is to have a function that returns a map which can have a Key type that changes depending on a function that's passed as a parameter to my function. 我的目标是拥有一个返回映射的函数,该映射的Key类型会根据作为参数传递给我的函数的函数而改变。
I have the following templated function (C++11) as the only compileable solution so far: 到目前为止,我具有以下模板化函数(C ++ 11)作为唯一可编译的解决方案:
template <typename Container, typename Function, typename KeyType>
std::map< KeyType, std::vector<typename Container::value_type> >
group_by(Container &container, Function function, KeyType keyHint) { ...
Right now this allows me to use the function this way: 现在,这允许我以这种方式使用该函数:
// the last parameter is just a type hint for the compiler
std::map<int, std::vector<int>> m = group_by(vec, func_that_returns_an_int, 1337);
...
std::map<int, std::vector<int>> m = group_by(vec, func_that_returns_an_int, -1);
// if we pass a function that returns bool, the map's key will be boolean
// the last parameter passed is just a hint for the compiler
std::map<bool, std::vector<int>> m = group_by(vec, is_even, true);
...
std::map<bool, std::vector<int>> m = group_by(vec, is_even, false);
My goal would be to not have to pass a random value to hint the compiler of what the Map's Key type should be, I wish I could do something like this: 我的目标是不必传递随机值来提示编译器Map的Key类型应该是什么,我希望我可以这样做:
std::map<int, std::vector<int>> m = group_by<int>(vec, func_that_returns_an_int);
...
std::map<bool, std::vector<int>> m = group_by<bool>(vec, is_even);
or is this even possible too? 还是有可能吗?
std::map<bool, std::vector<int>> m = group_by(vec, is_even);
I was playing with, auto and decltype to see if the compiler could be hinted by the left hand side of the operation with no luck. 我正在使用auto和decltype进行操作,以查看编译器是否可以由操作的左侧提示而没有运气。
auto group_by(Container &container, Function function) -> std::map< decltype(function(Container::value_type)), >
Changing order or template parameter would allow you to call it 更改顺序或模板参数将允许您调用它
group_by<int>(vec, func_that_returns_an_int);
So change 所以改变
template <typename Container, typename Function, typename KeyType>
std::map< KeyType, std::vector<typename Container::value_type> >
group_by(Container &container, Function function, KeyType keyHint)
to 至
template <typename KeyType, typename Container, typename Function>
std::map< KeyType, std::vector<typename Container::value_type> >
group_by(Container &container, Function function)
To remove completely KeyType
, you have to deduce it from other parameters: 要完全删除
KeyType
,必须从其他参数推导出它:
template <typename Container, typename Function>
auto group_by(Container &container, Function function)
-> std::map<decltype(function(*container.begin())),
std::vector<typename Container::value_type> >
You could use std::result_of<>
: 您可以使用
std::result_of<>
:
#include <map>
#include <vector>
#include <type_traits>
template <typename Container, typename Function, typename KeyType = std::result_of_t<Function()>>
auto group_by(Container c, Function f) -> std::map< KeyType, std::vector<typename Container::value_type>>
{
return // ...
}
int bar() { return 42; }
int main()
{
auto v = group_by(std::vector<double>{}, bar);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.