Вы не можете получить тип члена, но с помощью SFINAE можно добиться того, чего вы хотите, просто спросив, совпадает ли какой-либо тип с типом члена.
typedef char yes_type;
struct no_type { char arr[2]; };
template <typename Key, typename Entry>
struct is_key_of
{
private:
static yes_type test(Key Entry::*);
static no_type test(...);
public:
static bool const value = sizeof(test(&Entry::key)) == sizeof(yes_type);
};
template <bool> struct static_assertion;
template <> struct static_assertion<true> {};
#define OWN_STATIC_ASSERT(x) ((void)static_assertion<(x)>())
struct Key {
int a;
};
bool operator ==(const Key &key_1, const Key &key_2) {
return ( key_1.a == key_2.a );
}
struct Value {
int b;
};
struct Entry {
Key key;
Value val;
};
template <typename Entry>
class Table
{
public:
Table(){}
template <typename Key_T>
bool compareKeyWithEntry(const Entry& entry, const Key_T& key) {
OWN_STATIC_ASSERT((is_key_of<Key_T, Entry>::value));
return operator==(entry.key, key);
}
};
int main()
{
Entry e = { { 1 }, { 2 } };
Table<Entry> table;
table.compareKeyWithEntry(e, e.key);
//table.compareKeyWithEntry(e, 0); // static assertation raises
}
https://godbolt.org/z/V_N9ME
Вы можете заменить статическую диссертацию на enable_if
в типе возврата, если хотите устранить перегрузку, как в вашем вопросе decltype
.