我想为模板化类方法的返回类型添加别名,在 clang 上我使用
\ntemplate <class R, unsigned int BlockSize>\nstruct block {\n using KeyType = decltype(((R*)nullptr)->getKey());\n}\nRun Code Online (Sandbox Code Playgroud)\n这在 clang 上工作正常,但在 gcc 11.3.0 上我收到警告:
\n warning: \xe2\x80\x98this\xe2\x80\x99 pointer is null [-Wnonnull]\nRun Code Online (Sandbox Code Playgroud)\n我的问题是在没有警告的情况下获取 KeyType 的正确方法是什么?
\n为了能够在未计算的上下文(例如decltype表达式)中使用成员函数,应该使用std::declval:
template <class R, unsigned int BlockSize>
struct block {
using KeyType = decltype(std::declval<R>().getKey());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
67 次 |
| 最近记录: |