C++ Quiz104 of 173
Difficulty:
⬅️ Prev: 226Quiz ID: 227Next: 228 ➡️

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

#include <iostream>

using Func = int();

struct S {
    Func f;
};

int S::f() { return 1; }

int main() {
    S s;
    std::cout << s.f();
}
Hint

Func is a typedef-name for a function taking no parameters and returning an int.

Reveal answer

1

Explanation

using Func = int() is an alias-declaration introducing the typedef-name Func for a function taking no parameters and returning an int.

This typedef-name can be used both for free functions and, as in this question, for member functions. §[class.mfct]¶note-2 in the standard:

A member function can be declared (but not defined) using a typedef for a function type. The resulting member function has exactly the same type as it would have if the function declarator were provided explicitly, see §[dcl.fct].

The use of Func f in struct S is therefore equivalent to int f().

⬅️ Prev: 226Quiz ID: 227Next: 228 ➡️