#include <iostream>
struct A{
operator int(){
return 0;
}
};
int main(){
A a;
a; //#1
(int)a; //#2
(A)a; //#3
}
In the above example, #1
and #2
can both trigger Clang to emit the "expression result unused" warning but #3
does not.
What I am concerning here is that these three expressions are all discarded-value expressions, that is, the values of these glvalues are all discarded, as per
expr.context#2
In some contexts, an expression only appears for its side effects. Such an expression is called a discarded-value expression. The array-to-pointer and function-to-pointer standard conversions are not applied. The lvalue-to-rvalue conversion is applied if and only if the expression is a glvalue of volatile-qualified type and it is one of the following:
- [...]
If the (possibly converted) expression is a prvalue, the temporary materialization conversion is applied. The glvalue expression is evaluated and its value is discarded.
Presumably, Clang will emit the warning when the value of a glvalue is discarded.
At #1, the glvalue is discarded, hence Clang emits the warning.
At #2, since the expression is a prvalue, the materialization conversion applies to it to be a glvalue, and the value of such a glvalue is discarded. It has the same reason as that of #1
.
Now, consider #3, the effect likes static_cast<A>(a)
, which is a prvalue. Hence the materialization conversion still applies to it, then it becomes a glvalue, the same reason should make Clang emit the warning.
Although, "expression result unused" is not ruled by the standard, what I am concerning here is just whether it is reasonable that Clang didn't emit the warning for #3
.