C++ Quiz152 of 173
Difficulty:
⬅️ Prev: 313Quiz ID: 318Next: 319 ➡️

According to the C++23 standard, what is the output of this program?

#include <iostream>

struct S {
  operator void() {
    std::cout << "F";
  }
};

int main() {
  S s;
  (void)s;
  static_cast<void>(s);
  s.operator void();
}
Hint

A conversion function is never used to convert an object to void.

Reveal answer

F

Explanation

In this program, the object s is converted to void in three ways. First with a C-style cast, then with a static_cast, and finally by manually calling the conversion function.

According to the C++ standard, conversion functions will never be used to convert an object to void. So the conversion function is only called once, when we manually call it on the line s.operator void();.

§[class.conv.fct]¶4:

A conversion function is never used to convert a (possibly cv-qualified) object to the (possibly cv-qualified) same object type (or a reference to it), to a (possibly cv-qualified) base class of that type (or a reference to it), or to cv void.

⬅️ Prev: 313Quiz ID: 318Next: 319 ➡️