我正在使用一个 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");