C++ Quiz158 of 173
Difficulty:
⬅️ Prev: 327Quiz ID: 332Next: 333 ➡️

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

#include <iostream>

struct S {
    template <typename Callable>
    void operator[](Callable f) {
        f();
    }
};

int main() {
    auto caller = S{};
    caller[ []{ std::cout << "C";} ];
}
Hint

What does two [[consecutive]] square brackets usually mean? Could there be a parsing problem here?

Reveal answer

The program has a compilation error

Explanation

Two consecutive square brackets are normally used for [[attributes]], and indeed we are not allowed to use them like this here. §[dcl.attr.grammar]¶7 in the standard says:

Two consecutive left square bracket tokens shall appear only when introducing an attribute-specifier or within the balanced-token-seq of an attribute-argument-clause.

and even gives a similar example.

⬅️ Prev: 327Quiz ID: 332Next: 333 ➡️