【发布时间】:2018-04-27 18:20:38
【问题描述】:
我正在使用一个 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");
【问题讨论】:
标签: c++ templates typename result-of