3

Is the following program ill-formed according to the C++14 standard?

int f() { return 42; }

int main() {
  (void)f();
}

If not, is the sole function call expression contained within a discard-value expression? (note that is the subexpression, not the whole expression statement)

Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • 2
    Do you have any references that make you think `(void)f()` is ill-formed? – R Sahu Oct 24 '15 at 22:19
  • @RSahu: Well void is an incomplete type so: `void t = f();` is ill-formed, I would therefore expect `(void)x` to mean (without wording to the contrary) create a temporary of type void, which is equivalent to the ill-formed `void t = f();` previously mentioned. However in C this is well-formed, so I'm unclear. – Andrew Tomazos Oct 24 '15 at 22:21
  • @AndrewTomazos `void t` would not be a temporary (if it were legal). `void` is a valid type for an expression. For example a call to a void function. – M.M Oct 24 '15 at 22:28
  • You also see code like `(void)param;` to silence sometimes bogus warnings about un-used parameter `param` in a function, so the expression is definitely valid C++. – vsoftco Oct 24 '15 at 23:26
  • Also see [Returning a void?](http://stackoverflow.com/q/20478193/1708801) – Shafik Yaghmour Oct 25 '15 at 01:03

1 Answers1

8

Is the following program ill-formed according to the C++14 standard?

No. If you have some specific reason for thinking this might be invalid, you might be able to get a more detailed answer, but quoting each and every sentence of the standard in an attempt to point out that that sentence doesn't render the program invalid is not productive.

If not, is the sole function call expression contained within a discard-value expression?

The sole function call expression is the discarded-value expression.

5.2.9 Static cast [expr.static.cast]

6 Any expression can be explicitly converted to type cv void, in which case it becomes a discarded-value expression (Clause 5). [...]

I am assuming you are already aware that a C-style cast performs a static_cast if possible.

Community
  • 1
  • 1