Использование std::result_of<> для метода члена типа члена
Я работаю с типом HashMap, который не определяет свой KeyType в качестве открытого члена, только ValueType. Способ получить KeyType будет использовать std::result_of
с HashMap<>::Entry::GetKey()
метод. Я не могу заставить его работать в шаблоне, хотя.
template <typename K, typename V>
class Map {
public:
using ValueType = V;
class Entry {
public:
K GetKey();
};
};
Это отлично работает:
using M = Map<int, float>;
using T = std::result_of<decltype(&M::Entry::GetKey)(M::Entry)>::type;
static_assert(std::is_same<T, int>::value, "T is not int");
Но как бы я сделал это из шаблона, где M
такое параметр типа шаблона? Я попытался использовать вышеизложенное и вставить typename
ключевые слова без успеха.
template <typename M>
struct GetKeyType {
using T = std::result_of<decltype(&(typename M::Entry)::GetKey)(typename M::Entry)>::type;
};
using T = GetKeyType<Map<int, float>>::T;
static_assert(std::is_same<T, int>::value, "T is not R");
1 ответ
Решение
&M::Entry::GetKey
в целом, вы не должны разделять их typename
,
Следующий код будет работать:
template <typename M>
struct GetKeyType {
using T = typename std::result_of<decltype(&M::Entry::GetKey)(typename M::Entry)>::type;
};