【发布时间】:2020-05-17 08:02:56
【问题描述】:
考虑以下代码:
int main()
{
struct EmptyStruct{
void nonstatic_mf() const { std::cout <<"EmptyStruct\n"; }
};
EmptyStruct *esptr = nullptr;
esptr->nonstatic_mf();
}
这是一个合法的 C++(它似乎在 gcc 和 clang 中工作)?
【问题讨论】:
-
更有趣的问题是静态方法:-)
-
添加像
if (this != nullptr) ...这样的代码也不会很好地结束,因为对于优化的代码,编译器知道this永远不能为nullptr,所以代码就像if (true) ...。未定义的行为:不好。打开所有编译器警告是对抗未定义行为的一种方法。 -
@Jarod42 要我问吗?
-
因为简单地回答
[expr.post]/2不是那么有趣... -
@YSC 你已经回答了我的问题!非常感谢
标签: c++ pointers language-lawyer member-functions