CKTIS / C++
Value-initialization and empty braces `{}` in C++23
Empty braces do not always mean value-initialization. The type determines whether they initialize a scalar, call a constructor, or apply the rules for aggregates.
Version: ISO C++23; implementation support may vary
Syntax
T object{}; // Direct-list-initialization; the type determines the rule.
T object = {}; // Copy-list-initialization, not assignment.
int count{}; // Value-initialization: 0.
const char* next{}; // Value-initialization: null pointer.
char scratch[32]; // Default-initialization; automatic char elements are not initialized.
char ready[32]{}; // Aggregate initialization: all 32 elements are '\0'.
Result result{}; // Aggregate initialization when Result is the aggregate below.In C++23, T object{}; is direct-list-initialization with an empty list. T object = {}; is copy-list-initialization, not assignment. The label “value initialization ({})” describes what happens in cases such as int count{}; and const char* next{};, not every use of empty braces. For those scalars, zero-initialization gives 0 and a null pointer, respectively. A null pointer need not have an all-bits-zero representation.
Result result{}; and char ready[32]{}; use aggregate initialization. For a non-union aggregate, an omitted element uses its default member initializer if it has one; otherwise a non-reference element is copy-initialized from {}. An omitted reference with no initializer is an error. Thus result.count is 0, result.next is a null pointer, and every element of ready is \0.
When a class is value-initialized, it is default-initialized, with a preceding zero-initialization step only if the selected constructor is not user-provided. For example, after struct C { int n; C() {} }; C c{};, the local object's member c.n is still uninitialized.
In const char* input, * declares a pointer; const qualifies the characters, not the pointer. In the local declaration char scratch[32];, [32] declares an array of 32 characters whose elements are not initialized. Read only elements you have written. Before using the buffer as a C string, also write a terminating \0 within the array bounds.
Examples
#include <iostream>
struct Result {
int count;
const char* next;
};
int main() {
const char* input = "A";
char scratch[32];
char ready[32]{};
int count{};
Result result{};
scratch[0] = input[0];
std::cout << "copied: " << scratch[0] << '\n';
std::cout << "ready[31] is null character: " << (ready[31] == '\0') << '\n';
std::cout << "count: " << count << '\n';
std::cout << "result: " << result.count << ", null pointer: "
<< (result.next == nullptr) << '\n';
}Result
copied: A ready[31] is null character: 1 count: 0 result: 0, null pointer: 1
The program writes `scratch[0]` before reading it and prints one character, not the buffer as a string. The `ready[31]` comparison checks the last character, and `result.next == nullptr` checks the pointer value without dereferencing it.
Related lessons
- C++ · Lesson 47 · Advanced · Version: 3