Weekly Quiz number: 48 (2026)

Solve the quiz and collect tokens!

Type your nickname in the field below before submitting your solution. If you select the correct answer and submit it with your nick-name a token will be generated for you. Collect tokens and the corresponding nickname(s). If you get at least 10 of them you might be rewarded ;-)


Coding Quiz: C++20 Designated Initializers

C++20 designated initializers improve readability and clarity by letting you initialise aggregate members explicitly by name, as in Config c{.timeout = 30, .logging = true};. No more counting commas to work out which true was meant for what. Having names does not mean the order no longer matters, though. All snippets need -std=c++20.

Question 1: After timeoutMs is inserted into Retry, what does the program print?

#include <iostream>

struct Retry {
    int attempts = 3;
    int timeoutMs = 1000;  // newly inserted member
    int delayMs = 100;
};

int main() {
    Retry a{5, 200};                         // written before the change
    Retry b{.attempts = 5, .delayMs = 200};  // written before the change
    std::cout << a.delayMs << ' ' << b.delayMs << ' ' << b.timeoutMs << '\n';
}
It prints 100 200 0: in a the 200 now lands in timeoutMs; b still sets delayMs, and its skipped timeoutMs is zero-initialised.
It prints 100 200 1000: in a the 200 now lands in timeoutMs; b still sets delayMs, and its skipped timeoutMs keeps its default member initialiser.

Question 2: A colleague adds Config() = default; to a working aggregate. What happens to the designated initialisation in main?

struct Config {
    Config() = default;  // added "just to be explicit"
    int timeout = 30;
    int retries = 3;
    bool logging = false;
};

int main() {
    Config c{.timeout = 60, .logging = true};
    return c.retries;
}
It does not compile: in C++20 any user-declared constructor, even a defaulted one, means Config is no longer an aggregate.
It compiles and returns 3: a defaulted constructor is not user-provided, so Config remains an aggregate and accepts designators.

Question 3: Which plot overload does the call plot({.r = 1.0, .phi = 0.5}) select?

#include <iostream>

struct Cartesian { double x; double y; };
struct Polar     { double r; double phi; };

void plot(Cartesian) { std::cout << "Cartesian\n"; }
void plot(Polar)     { std::cout << "Polar\n"; }

int main() {
    plot({.r = 1.0, .phi = 0.5});
}
It does not compile: both overloads take an aggregate of two doubles, so the call is ambiguous, just like plot({1.0, 0.5}).
It prints Polar: only Polar has members named r and phi, so the Cartesian overload is not viable for this call.

Software Design Meeting

Shape the agenda of our Software Design Meeting! Submit topics that you would like to dive deeper into: Software Design Meeting: Topic Ideas Survey (https://bos.ch/vhzwinp)

The coaching team holds a software design meeting every Thursday. It is open to anyone looking to expand their knowledge and connect with like-minded peers. (To receive an invitation, please reach out to Milosz Walter (XC-AS/ESE4) or visit https://bos.ch/join-sw-design-meeting)

E-Mail Reminder

Want a quick email notification in your inbox when the next quiz is live? Visit https://bos.ch/coding-quiz-reminder

 

 


Make sure your nickname contains at least 8 characters and is a unique as possible.




Disclaimer: