在没有警告的情况下确定类方法的返回类型



我想为模板化的类方法别名返回类型,在clang上我使用

template <class R, unsigned int BlockSize>
struct block {
using KeyType = decltype(((R*)nullptr)->getKey());
}

这在clang上工作得很好,但是在gcc 11.3.0上我得到一个警告:

warning: ‘this’ pointer is null [-Wnonnull]

我的问题是什么是正确的方式来获得KeyType没有警告?

为了能够在未求值的上下文中使用成员函数,例如decltype表达式,应该使用std::declval:

template <class R, unsigned int BlockSize>
struct block {
using KeyType = decltype(std::declval<R>().getKey());
}

最新更新