Why is `decltype(static_cast<T>(…))` not always `T`?

此生再无相见时 提交于 2019-12-03 04:36:18

This is hard-coded in the definition of static_cast:

[expr.static.cast] (emphasis mine)

1 The result of the expression static_­cast<T>(v) is the result of converting the expression v to type T. If T is an lvalue reference type or an rvalue reference to function type, the result is an lvalue; if T is an rvalue reference to object type, the result is an xvalue; otherwise, the result is a prvalue. The static_­cast operator shall not cast away constness.

decltype respects the value category of its operand, and produces an lvalue reference for lvalue expressions.

The reasoning may be due to function names themselves always being lvalues, and so an rvalue of a function type cannot appear "in the wild". As such, casting to that type probably makes little sense.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!