-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesignated-init-cpp20.cpp
More file actions
41 lines (29 loc) · 1.06 KB
/
designated-init-cpp20.cpp
File metadata and controls
41 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wunused-value"
#define CATCH_CONFIG_MAIN // Tells Catch2 to provide a main()
#include "../catch/catch_amalgamated.hpp"
#include "utils.h"
using namespace std;
// designated init.
// - Designated initializers let you initialize struct members by name instead
// of position.
//
// Key notes:
// - Best pattern: use as named function arguments via a params struct
// - Aggregate types only (types w/ no constructors, no private members)
// - Must follow declaration order — skipping is fine, reordering is not
// - Skipped fields are zero/default initialized
TEST_CASE("des-ini-1") {
// before cpp20 — positional, fragile
struct Config { int width, height, depth; bool vsync; };
Config c = {1920, 1080, 32, true};
// which is width? which is depth? easy to swap ❌
// cpp20 — named, self-documenting
Config c_ = {
.width = 1920,
.height = 1080,
.depth = 32,
.vsync = true
};
REQUIRE(c_.width == 1920);
}