C++
C++ Narrowing Conversion Error Fix
The same conversion that copy initialization performs silently - dropping the fraction from 3.14 without a word - becomes a hard error inside braces. This guide separates the five shapes GCC rejects as narrowing conversions, reads what each diagnostic actually names, and matches each shape to its repair: state the conversion explicitly, widen the target type, or change the value so it fits.
Written November 24, 2026 · Tested with GCC 13.3.0 · 8 min read
- The rule underneath: braces refuse conversions that can lose values
- How to read the diagnostic
- The first failure: a double literal in an int's braces
- The second failure: a constant too wide for a char
Written November 24, 2026
Tested with GCC 13.3.0
What this guide is
Without braces, converting 3.14 to an int quietly produces 3. Inside braces, the same conversion stops the compile. List initialization refuses conversions that can lose information - narrowing conversions - and that single rule produces a family of errors that look unrelated until the rule is visible: a double literal in an int variable's braces, an int constant too wide for a char, a negative constant in an unsigned int, a braced argument passed to an int parameter, and a double constant too large for a float.
The five forms in this guide each reproduce the failure with a small, complete program. In each family the failing program is small, the full compiler output is shown, and the fix keeps the program's intent while removing the narrowing: an explicit cast where the loss is intended, a wider target type where the value was the point, or a value that fits where the original could not. Four neighboring shapes in the traps are not this error at all: the same conversion written with an equals sign or with parentheses compiles without any diagnostic, a narrowing conversion from an ordinary variable draws only a warning from GCC rather than an error - a const or constexpr integer variable whose value does not fit is a constant expression, and errors like a literal, and a constant double converted to a float compiles when the result stays finite. One further program fills five variables in braces with constants that fit their types exactly, and compiles: that is the boundary of this error.
Build every program in this guide with g++ -std=c++20. In the compiler transcripts below, the file names are the bare names shown on the command line.
The rule underneath: braces refuse conversions that can lose values
List initialization is initialization from a brace-enclosed list, and it applies in more places than variable declarations: it also initializes a function parameter when a braced list is passed as an argument, and the returned object when a braced list is the return expression. In those contexts only non-narrowing conversions are allowed - for a non-class target initialized from a single value in braces, narrowing conversions are not allowed, and a program that needs one is ill-formed.
The same page defines which conversions count as narrowing. A conversion from a floating-point type to an integer type is narrowing. A conversion from one floating-point type to another of lower rank is narrowing, with an exception: when the source is a constant expression, the converted value is finite, and the conversion does not overflow, the conversion is allowed - the fourth trap below is that exception at work, and the fifth family is what happens when the overflow condition fails. A conversion from an integer type to a floating-point type is narrowing except where the source is a constant expression whose value can be stored exactly in the target type. And a conversion from an integer type to an integer type that cannot represent all values of the original is narrowing, except where the source is a constant expression whose value can be stored exactly in the target type - the boundary program lives on that exception, and the second and third families are what happens when the constant does not fit.
Outside braces, none of this applies. The ordinary implicit conversions run as they always have: a double used where an int is expected - as a function argument, as an initializer, in a return statement - is converted, fraction dropped, with no diagnostic required. That contrast is the practical content of this guide. Braces are the one initialization form that checks the conversion against the target's range before allowing it, which is why the fix is so often to keep the braces and change something else.
One honesty note about the compiler in this guide, because the traps depend on it: GCC reports the narrowing conversions in the five families as errors, and each failing program below shows exactly one. When the source value is not a constant expression - an ordinary variable rather than a literal - GCC 13.3 prints the same narrowing diagnostic as a warning and compiles the program, as the third trap shows. A variable can still be a constant expression: a const or constexpr integer variable whose value does not fit is an error, exactly like the literal, so the line GCC draws is constant expression versus not, not variable versus literal. The standard's rule does not draw that line; this compiler's diagnostic severity does. Code that must build cleanly under other compilers, or under stricter flags, should be fixed the same way either way.
How to read the diagnostic
Every error in the five families has the same sentence shape: "error: narrowing conversion of" the source value, "from" the source type, "to" the target type, tagged [-Wnarrowing]. Three facts are on the one line, and each one points at a different part of the repair. The source value tells you what was written - a literal you can change, in every family here. The source type and the target type tell you which side of the conversion is wrong for the program's intent: if the value matters, the target type is the side to change; if the target type matters, the value has to be converted deliberately, in the open, with a cast.
The line and column locate the braces, not the declaration as a whole. In the argument family the diagnostic sits on the call, at the braced argument, even though the parameter it fails to initialize is declared in the function above it. Read the location as "the conversion happens here," then read the two types to decide the fix.
There is no note under these errors, unlike errors that pair two declarations. A narrowing error is self-contained: one line, one conversion, one decision - keep the value and widen the target, keep the target and convert the value explicitly, or replace the value with one the target can hold.
The first failure: a double literal in an int's braces
A double literal initializing an int in braces:
#include <iostream>
int main() {
int x{3.14};
std::cout << x << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fail_double_to_int fail_double_to_int.cpp
fail_double_to_int.cpp: In function ‘int main()’:
fail_double_to_int.cpp:4:11: error: narrowing conversion of ‘3.1400000000000001e+0’ from ‘double’ to ‘int’ [-Wnarrowing]
4 | int x{3.14};
| ^~~~
The diagnostic names the value in scientific notation, 3.1400000000000001e+0, from double to int. Conversion from a floating-point type to an integer type is on the narrowing list with no constant exception in the list-initialization rules: a floating value is never carried into an integer through braces, because the fraction has nowhere to go.
The intent behind this shape is usually one of two things: the variable was meant to be a double, or the truncation was meant to happen. The fix below takes the second reading - the program wants the whole-number part, stated openly. The value is held in a double variable and converted with static_cast, which is an explicit conversion, not an implicit one, so the narrowing rule does not apply to it:
#include <iostream>
int main() {
double value = 3.14;
int x{static_cast<int>(value)};
std::cout << x << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fix_double_to_int fix_double_to_int.cpp
$ ./fix_double_to_int
3
The output is 3: the fraction dropped, as the failing program intended, but now the drop is written in the code where a reader can see it. If the intent had been the first reading, the fix is to change the variable's type to double and keep the value whole. What does not work is leaving the braces around the literal and hoping for the silent truncation that copy initialization performs - braces exist, in part, to refuse exactly that.
The second failure: a constant too wide for a char
An int constant initializing a char in braces:
#include <iostream>
int main() {
char c{1000};
std::cout << static_cast<int>(c) << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fail_int_to_char fail_int_to_char.cpp
fail_int_to_char.cpp: In function ‘int main()’:
fail_int_to_char.cpp:4:12: error: narrowing conversion of ‘1000’ from ‘int’ to ‘char’ [-Wnarrowing]
4 | char c{1000};
| ^~~~
The diagnostic reads "narrowing conversion of '1000' from 'int' to 'char'". A char cannot represent all values of an int, so the conversion is narrowing - and the constant-expression exception does not save it, because the exception applies only where the value can be stored exactly in the target type, and 1000 cannot be stored in a char. The value is the problem, not the syntax: the same declaration with a constant that fits, such as 65, compiles, as the boundary program shows.
Here the value was the point - the program wants the number 1000 - so the fix widens the target type to one that holds it:
#include <iostream>
int main() {
short code{1000};
std::cout << code << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fix_int_to_char fix_int_to_char.cpp
$ ./fix_int_to_char
1000
The output is 1000, held whole in a short. If the variable really must be a char - a byte in a buffer, a field in a packed layout - then the value has to change instead: either use a constant the type can hold, or, when the source value is computed rather than written, convert it explicitly with static_cast after checking its range, accepting that the cast makes the conversion your responsibility rather than the compiler's.
The third failure: a negative constant in an unsigned variable
A negative constant initializing an unsigned int in braces:
#include <iostream>
int main() {
unsigned int count{-1};
std::cout << count << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fail_negative_unsigned fail_negative_unsigned.cpp
fail_negative_unsigned.cpp: In function ‘int main()’:
fail_negative_unsigned.cpp:4:24: error: narrowing conversion of ‘-1’ from ‘int’ to ‘unsigned int’ [-Wnarrowing]
4 | unsigned int count{-1};
| ^~
The diagnostic reads "narrowing conversion of '-1' from 'int' to 'unsigned int'". An unsigned int cannot represent all values of an int - the negative half of the range is missing - so the conversion is narrowing, and -1 is not a constant that can be stored exactly in the target type. Without braces this same declaration would compile and produce the largest unsigned int value, the wraparound result of converting -1; the braces refuse it before it can happen.
The usual intent is a sentinel: -1 meaning "none" or "not found". That meaning belongs to a signed type, so the fix changes the target type and keeps the value:
#include <iostream>
int main() {
int count{-1};
std::cout << count << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fix_negative_unsigned fix_negative_unsigned.cpp
$ ./fix_negative_unsigned
-1
The output is -1, represented exactly. When the variable genuinely must stay unsigned - an interface fixes its type - the sentinel has to be expressed in the type's own terms, for example as the type's maximum value obtained from its limits, rather than as a negative number the type cannot hold. Either way, the repair names the sentinel in a type that can represent it.
The fourth failure: a braced argument to an int parameter
A braced double passed to a function whose parameter is an int:
#include <iostream>
void show(int value) {
std::cout << value << '\n';
}
int main() {
show({3.14});
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fail_braced_arg fail_braced_arg.cpp
fail_braced_arg.cpp: In function ‘int main()’:
fail_braced_arg.cpp:8:9: error: narrowing conversion of ‘3.1400000000000001e+0’ from ‘double’ to ‘int’ [-Wnarrowing]
8 | show({3.14});
| ~~~~^~~~~~~~
List initialization is not only for variable declarations: passing a brace-enclosed list as a function argument list-initializes the parameter, so the narrowing rule applies at the call. The diagnostic sits on the call line and names the same conversion as the first family, from double to int - the parameter's type is the target, even though the parameter is declared three lines above the error.
The contrast that makes this family surprising is in the traps: the same call without braces, show(3.14), compiles and prints 3, because an ordinary function argument is converted by the implicit conversion rules with no narrowing check. The braces are what add the check.
The fix passes a value of the parameter's type inside the braces:
#include <iostream>
void show(int value) {
std::cout << value << '\n';
}
int main() {
show({3});
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fix_braced_arg fix_braced_arg.cpp
$ ./fix_braced_arg
3
The output is 3. If the call site really holds a double variable, the matching repairs are the first family's: convert it explicitly with static_cast at the call, or change the parameter's type if the function was meant to receive the fractional value. The same rule governs a braced return expression - a function returning int whose return statement hands back a braced double list-initializes the returned object and fails the same way - because the return statement is one of the list-initialization contexts.
The fifth failure: a double constant too large for a float
A double constant initializing a float in braces:
#include <iostream>
int main() {
float f{1e300};
std::cout << f << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fail_double_to_float fail_double_to_float.cpp
fail_double_to_float.cpp: In function ‘int main()’:
fail_double_to_float.cpp:4:13: error: narrowing conversion of ‘1.0000000000000001e+300’ from ‘double’ to ‘float’ [-Wnarrowing]
4 | float f{1e300};
| ^~~~~
The diagnostic reads "narrowing conversion of '1.0000000000000001e+300' from 'double' to 'float'". Conversion from a floating-point type to one of lower rank is narrowing, and the constant-expression exception has conditions: the converted value must be finite and the conversion must not overflow. The value 1e300 overflows a float, whose range ends far below it, so the exception does not apply and the conversion is refused. The fourth trap shows the same conversion succeeding when the conditions hold, which is why this family is about the value's size, not about double and float as a pair.
The value was the point again, so the fix widens the target type to the one that holds it:
#include <iostream>
int main() {
double f{1e300};
std::cout << f << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fix_double_to_float fix_double_to_float.cpp
$ ./fix_double_to_float
1e+300
The output is 1e+300, the value held in a double. If the variable must stay a float - an array of them, a graphics buffer - then the value has to come down into the type's range first, and any conversion that still loses range or precision should be written as an explicit cast so the loss is visible at the site. A float that silently became infinity is the failure the braces prevented here.
The fixes in reach order
- A floating value in an integer's braces: decide whether the fraction matters. If it does, change the target type to a floating type; if it does not, hold the value in a variable and convert with
static_castso the truncation is explicit. - An integer constant too wide for the target: keep the value and widen the target type, or keep the type and use a constant it can store exactly.
- A negative value in an unsigned variable: move the sentinel to a signed type, or express it as a value the unsigned type can represent.
- A braced argument or return value: pass or return a value of the parameter or return type inside the braces, convert explicitly with
static_cast, or change the declared type if the fractional value was intended. - A floating constant that overflows a smaller floating type: widen the target type, or bring the value into range before converting - and write any remaining lossy conversion as a cast.
- In every case: read the two types on the diagnostic line first. They tell you which side of the conversion - value or target type - the repair belongs on.
Traps
- The same conversion with an equals sign is copy initialization, not list initialization, and it compiles without any diagnostic at all:
#include <iostream>
int main() {
int x = 3.14;
std::cout << x << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o probe_copy_init probe_copy_init.cpp
$ ./probe_copy_init
3
3. No error, and no warning in the transcript either: the fraction is dropped silently, exactly the behavior the braces refuse. This is the shape the narrowing rule exists to catch, and it is worth recognizing in the other direction - code that compiles is not necessarily code where the conversion was intended.
- Parentheses are not braces. Direct initialization with parentheses performs the same silent conversion:#include <iostream>
int main() {
int x(3.14);
std::cout << x << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o probe_paren_init probe_paren_init.cpp
$ ./probe_paren_init
3
The output is 3, again with no diagnostic. Only the brace form carries the narrowing check, so changing an equals sign or parentheses to braces is a real audit of a declaration, not a style edit: declarations that survive the change unchanged are ones where no lossy implicit conversion was hiding.
- A narrowing conversion from a variable draws a warning from GCC, not an error. Here an int variable holding 65 initializes a char in braces:
#include <iostream>
int main() {
int code = 65;
char c{code};
std::cout << c << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o probe_var_to_char probe_var_to_char.cpp
probe_var_to_char.cpp: In function ‘int main()’:
probe_var_to_char.cpp:5:12: warning: narrowing conversion of ‘code’ from ‘int’ to ‘char’ [-Wnarrowing]
5 | char c{code};
| ^~~~
$ ./probe_var_to_char
A
The diagnostic is "warning: narrowing conversion of 'code' from 'int' to 'char'", and the program then compiles, runs, and prints A. The conversion is the second family's conversion - an int cannot be represented in full by a char - but because the source here is an ordinary variable, not a constant expression, GCC 13.3 reports it at warning severity - a const or constexpr integer variable holding a value that does not fit would be an error instead, because it is a constant expression. Do not read the warning as permission: the value 65 fits here, but nothing in the compiled program checks the next value the variable might hold, and other compilers treat the same code as an error. The repairs are the second family's - widen the type, or cast explicitly after a range check.
- A constant double in a float's braces compiles when the result stays finite. This is the constant-expression exception for floating-to-floating conversion at work:
#include <iostream>
int main() {
float f{3.141592653589793};
std::cout << f << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o probe_double_to_float_const probe_double_to_float_const.cpp
$ ./probe_double_to_float_const
3.14159
The output is 3.14159: the value rounded to float precision, with no diagnostic. The exception's conditions are that the source is a constant expression, the converted value is finite, and the conversion does not overflow - all three hold here, and the fifth family is the same shape with the overflow condition broken. Note what the exception does not promise: the stored value is the nearest float, not the written constant, so precision is still lost - legally, and silently.
- Constants that fit are the boundary of the error. This program list-initializes five variables, and every source is a constant expression whose value can be stored exactly in its target type:
#include <iostream>
int main() {
char c{65};
short s{1000};
unsigned int u{42};
float f{42};
int n{7};
std::cout << c << ' ' << s << ' ' << u << ' ' << f << ' ' << n << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o boundary_fits boundary_fits.cpp
$ ./boundary_fits
A 1000 42 42 7
The output is A 1000 42 42 7: the char holds the character for 65, the short holds 1000, the unsigned int holds a non-negative constant, the float holds 42 - an integer constant stored exactly, which is the integer-to-floating exception - and the int holds 7. Braces do not reject conversions wholesale; they reject the ones that can lose values, and a constant that provably fits loses nothing. The practical test for any braced declaration you meet: is the source a constant whose value the target can store exactly, a variable of a type the target can fully represent, or an explicit cast? Those pass. Everything else is one of the five families or the warning in the third trap.
Checklist
- The diagnostic line read for all three facts: the source value, the source type, and the target type, with the repair chosen on the side - value or target type - that the program's intent points to.
- No floating value left initializing an integer through braces; intended truncations written as
static_cast, intended fractions given a floating target type. - Integer constants checked against the target's range, not its usual values: a constant the type cannot store exactly is an error even when the declaration looks ordinary.
- No negative sentinel written into an unsigned variable in braces; sentinels live in signed types or are expressed as values the unsigned type can hold.
- Braced arguments and braced return expressions checked with the same rule as braced variable declarations - the parameter and return types are targets too.
- Floating-to-floating braces checked for overflow, not just precision: a finite constant that fits compiles, an overflowing one is an error.
- GCC's warning for a variable source treated as the same defect at lower severity, and fixed rather than compiled past.
- Copy-initialized and parenthesized declarations not mistaken for checked ones: only braces apply the narrowing rule.
- The fixed program run, and its output checked against what the failing program was trying to compute.
Related guides on this site
- R_X86_64_PC32 Relocation Error Fix
- R_X86_64_32 Relocation Error Fix
- C++ Invalid Conversion Void* Fix
- C++ Redefinition Error Fix
Sources
- cppreference, List initialization - for list initialization initializing variables, function parameters, and returned objects from brace-enclosed lists, for only non-narrowing conversions being allowed in those contexts, and for the definition of narrowing conversions: floating-point to integer, floating-point to a lower-ranked floating-point type with the constant-expression exception for finite non-overflowing results, integer to floating-point except constants stored exactly, and integer to an integer type that cannot represent all values of the original except constants stored exactly.
- cppreference, Implicit conversions - for implicit conversions being performed when an expression is used as a function argument, when initializing a new object, and in a return statement, and for the standard conversion sequences that perform them outside braces without a narrowing check.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.