skip to content

Why does C++ reject int width{1920 * 1.25}; while int width = 1920 * 1.25; compiles?

level: middleimportance: must knowfreq 65%

answer

  1. braces check the conversion
  2. list-initialization forbids narrowing
  3. floating to integer always narrows
  4. constants that fit are allowed
  5. only at the top level of the list

basics

~20 s

Braces mean list-initialization, which makes a narrowing conversion ill-formed. Converting a double to an int is always narrowing, even from a constant, so the braced form is rejected, while copy-initialization with = silently truncates 2400.0 to 2400.

solid answer

~40 s

Brace forms are **list-initialization**, and list-initialization makes a **narrowing conversion** ill-formed. Narrowing covers floating to integer (always, even for constants), a floating type to one of lower rank, integer to floating, integer to an integer type that cannot hold all source values, and, since C++20, pointer to `bool`. The floating-to-floating and integer conversions are excused for a **constant expression** whose value fits, so `std::uint16_t r{144};` compiles but `std::uint16_t r{-1};` does not. The rule applies only at the top level of the list — `{f(2.0)}` for `f(int)` still compiles. Copy-initialization with `=`, parentheses and function arguments keep C's silent conversions. Note that compilers diagnose differently: Clang makes narrowing an error, while GCC makes only constant narrowing an error and gives a warning for non-constant narrowing.

code

cpp · 17 lines
cpp
#include <cstdint>

struct WindowConfig {
    int width = 1280;
    int height = 720;
    float ui_scale = 1.0f;
    bool vsync = true;
};

int main() {
    double scale = 1.25;
    int from_copy = 1920 * scale;              // compiles: silently truncates
    WindowConfig ok{1920, 1080, 1.5f, false};  // every value fits exactly
    std::uint16_t refresh{144};                // constant that fits: allowed
    WindowConfig bad{1920 * scale, 1080};      // error: double -> int narrowing
    (void)from_copy; (void)ok; (void)refresh; (void)bad;
}

go deeper

for a junior

Remember that braces refuse to convert a double into an int, while = and parentheses truncate silently.

for a middle

List the narrowing categories, the constant-expression exception, and why the check applies only at the top level of the list.

for a senior

Explain the differing compiler diagnostics and the C++20 parenthesized aggregate gap, and use static_cast to make intended truncation explicit.

for a principal

Decide whether braces are the house style for member initialization, given narrowing protection against the initializer-list and aggregate surprises.

## What "narrowing" means A **narrowing conversion** is an implicit conversion that can lose information. The standard lists exactly which ones count: 1. **Floating to integer**, always — `int w{2400.0}` is narrowing even though 2400.0 is a whole number. 2. **Floating to a floating type of lower conversion rank** (`double` to `float`), unless the source is a constant expression whose value is finite and does not overflow; `float s{0.1};` is fine, `float s{d};` for a `double` variable is not. 3. **Integer or unscoped enumeration to floating**, unless the source is a constant expression that converts exactly and back: `float f{7};` is fine, `float f{n};` for an `int` variable is not. 4. **Integer to an integer type that cannot represent every source value**, unless the source is a constant expression whose value fits, or a bit-field narrow enough to fit. 5. **Pointer or pointer-to-member to `bool`** (added in C++20). ## Why braces care and `=` does not All the brace forms — `T x{a}`, `T x = {a}`, `T{a}`, `new T{a}`, a member list `WindowConfig{a, b}` — are **list-initialization**, and list-initialization makes narrowing at the top level of the list ill-formed. Every other form keeps the silent conversions C++ inherited from C: | Form | `int` from `double` 2400.0 | |---|---| | `int w = 1920 * 1.25;` | compiles, value 2400 | | `int w(1920 * 1.25);` | compiles, value 2400 | | `int w{1920 * 1.25};` | ill-formed: narrowing | | `WindowConfig c{1920 * scale, 1080};` | ill-formed: narrowing in the aggregate's member list | | `WindowConfig c(1920 * scale, 1080);` (C++20) | compiles, truncates — parenthesized aggregate initialization allows narrowing | The C++20 row surprises people: parenthesized aggregate initialization was added so that `make_unique`, `emplace_back` and similar forwarding functions can build aggregates, and it deliberately keeps parentheses' permissive semantics — no narrowing check, no designators, no brace elision. ## The constant-expression exception Integer and integer-to-floating narrowing is excused when the value is known at compile time and fits: - `std::uint16_t refresh{144};` compiles: 144 fits. - `std::uint16_t refresh{-1};` does not: Clang says "constant expression evaluates to -1 which cannot be narrowed to type 'std::uint16_t' (aka 'unsigned short')". - `const int k = 300; unsigned char c{k};` does not compile on a platform with 8-bit bytes: `k` is a constant expression, but 300 does not fit. - `std::int64_t big = 70; std::uint16_t h{big};` does not compile even though 70 would fit: `big` is not a constant expression, so the compiler must assume any value. ## Only the top level The check applies to the conversion from each list element to its target, not to expressions inside the element. `int a[] = {2, f(2.0)};` compiles when `f` takes an `int`: the `double` argument converts inside an ordinary function call. By contrast, in `std::vector<int>{1, 2, 3}` each element is itself a top-level item, converted to `int` in the backing array of the `std::initializer_list<int>`, so a `2.5` there is narrowing and rejected. ## What compilers actually do The standard requires a diagnostic; it does not require an error. - **Clang** rejects narrowing with an error such as "type 'double' cannot be narrowed to 'int' in initializer list [-Wc++11-narrowing]". - **GCC** documents that a narrowing conversion from a constant produces an error and one from a non-constant produces a **warning** by default, with `-Wno-narrowing` silencing it. So code that narrows a run-time value can build with a warning in one toolchain and fail in another. Teams that build with several compilers should treat the warning as an error. ## Why this matters in review - Braces turn silent truncation (a window width computed from a DPI scale, a reading converted to a smaller field) into a compile-time decision. - When the truncation is intended, `static_cast<int>(1920 * scale)` states it and keeps the braces' protection for everything else. - Narrowing is one reason to prefer braces for member initialization in configuration structs; the other brace rules (initializer-list preference, aggregate rules) are the reason not to use them blindly.

  • Does a braced initializer stop narrowing inside a function call within the list?
    No. The narrowing check applies only to converting each list element to its target. In `int a[] = {2, f(2.0)};` with `f(int)`, the `double` argument converts as an ordinary function argument and compiles; only the result of `f` is checked against the array's element type, and `int` to `int` is not narrowing.
  • Why does C++20's WindowConfig cfg(1920.5, 1080); compile when the braced version does not?
    C++20 added parenthesized aggregate initialization so forwarding functions such as `std::make_unique` and `emplace_back` can construct aggregates. It keeps the semantics of parentheses: narrowing conversions are allowed, designators are not, there is no brace elision, and a temporary bound to a reference member is not lifetime-extended. So `1920.5` silently truncates to 1920.

  • int w{2400.0} compiles because 2400.0 is a whole number
  • braces forbid every implicit conversion
  • narrowing is always a hard error on every compiler
  • a const variable is always exempt from the narrowing check
  • parentheses and braces check aggregate members the same way in C++20

Assumes C++20 or later: pointer-to-bool became narrowing in C++20 and parenthesized aggregate initialization, which allows narrowing, is C++20; C++23 restated floating narrowing in terms of conversion rank.