A type conversion where the destination type which cannot represent all values in the source type.
Questions tagged [narrowing]
167 questions
81
votes
4 answers
Declaring a variable with two types: "int char"
I'm a C++ beginner, and I'm reading Bjarne Stroustrup's Programming: Principles and Practice Using C++.
In the section on 3.9.2 Unsafe conversions, the author mentioned
When the initializer is an integer literal, the compiler can check the actual…

Thor
- 9,638
- 15
- 62
- 137
45
votes
3 answers
Why isn't a final variable always a constant expression?
In the below code:
final int a;
a=2;
byte b=a; // error: possible loss of precision
Why do I get this error? Isn't a final variable compile time constant expression and hence implicitly narrowed to byte during the assignment?
In other words isn't…

paidedly
- 1,413
- 1
- 13
- 22
41
votes
1 answer
Emacs: same buffer, two windows, one narrowed, one not
I find the narrow-to-region command useful, however it applies to the buffer and not to the current window.
I'd like to have one window display a narrowed version of the buffer, while the buffer is displayed widened if it occurs in any other…

EoghanM
- 25,161
- 23
- 90
- 123
34
votes
4 answers
How does implicit conversion work in Java?
I know that in Java Integer literals are int by default,so if I write something like this
byte byteValue = 2;
Java auto converts the literal value 2(which is an int by default) to byte.
And the same thing works if I write
byte byteValue =…

mohsin ahmed
- 341
- 2
- 4
28
votes
2 answers
warning: narrowing conversion C++11
g++ 4.9.0
-O2 -std=c++11
template
struct vec3 {
T x, y, z;
vec3() = default;
vec3(const vec3 &other) = default;
vec3(T xx, T yy, T zz) { x = xx; y = yy; z = zz; }
vec3 operator-(const vec3 &other) {
…

hidayat
- 9,493
- 13
- 51
- 66
26
votes
1 answer
Why can't I initialize this std::vector with an l-value?
I've come across an interesting issue, and I can't understand what's happening:
/* I WANT 6 ELEMENTS */
int lvalue = 6;
std::vector myvector { 6 }; /* WORKS FINE */
std::vector myvector{ lvalue }; /* DOESN'T WORK */
/* Element '1':…

Zebrafish
- 11,682
- 3
- 43
- 119
25
votes
1 answer
Narrowing conversion to bool in list-initialization - strange behaviour
Consider this piece of C++11 code:
#include
struct X
{
X(bool arg) { std::cout << arg << '\n'; }
};
int main()
{
double d = 7.0;
X x{d};
}
There's a narrowing conversion from a double to a bool in the initialization of x.…

bogdan
- 9,229
- 2
- 33
- 48
18
votes
2 answers
Type safe way of narrowing type of arrays by length when noUncheckedIndexedAccess is true
Given a function that takes a single string[] argument myArray.
If I evaluate the .length property and that property is greater than 0, then (assuming my variable isn't any in disguise) its impossible for myArray[0] to be undefined. However, if I…

Ben Wainwright
- 4,224
- 1
- 18
- 36
17
votes
4 answers
Is there a "safe" static_cast alternative?
Is there a "safe" alternative to static_cast in C++11/14 or a library which implements this functionality?
By "safe" I mean the cast should only allow casts which do not lose precision. So a cast from int64_t to int32_t would only be allowed if the…

Andreas Pasternak
- 1,250
- 10
- 18
17
votes
3 answers
Why does a narrowing conversion warning appear only in case of list initialization?
I have the following code:
class A
{
public:
A(const unsigned int val) : value(val) {}
unsigned int value;
};
int main()
{
int val = 42;
A a(val);
A b{val}; // <--- Warning in GCC, error in Microsoft Visual…

Alex
- 9,891
- 11
- 53
- 87
16
votes
2 answers
Why doesn't narrowing conversion used with curly-brace-delimited initializer cause an error?
I learnt about curly-brace-delimited initializer in The C++ Programming Language, 4th ed. > Chapter 2: A Tour of C++: The Basics.
I am quoting from the book below.
The = form is traditional and dates back to C, but if in doubt, use the general {}…

Lone Learner
- 18,088
- 20
- 102
- 200
15
votes
7 answers
Using -1 to initialize an unsigned in { } initialization of struct or array
REALLY BRIEF
How do you create an unsigned constant that has all bits set?
...that you can use to initialize a field with { }s,
...that does not get a -Wnarrowing warning from GCC 4.7.2.
The following are not satisfactory:
struct U { unsigned…

Krazy Glew
- 7,210
- 2
- 49
- 62
14
votes
1 answer
Why is const int fine for char brace init?
I thought brace initialization doesn't allow narrowing. But why is int const allowed for char brace initialization?
int value1 = 12;
char c1{value1}; // error! no narrowing
const int value2 = 12;
char c2{value2}; // why is this fine?
See it on…

BK C.
- 573
- 2
- 7
- 16
12
votes
2 answers
Understanding gsl::narrow implementation
The C++ Core Guidelines has a narrow cast that throws if the cast changes the value. Looking at the microsoft implementation of the library:
// narrow() : a checked version of narrow_cast() that throws if the cast changed the value
template

bolov
- 72,283
- 15
- 145
- 224
12
votes
1 answer
C++: using curly braces to prevent narrowing during assignment
I'm familiar with using curly braces/ initializer lists to prevent narrowing when initializing a variable, but is it good practice to use it when assigning a value to a variable too?
For e.g.
int i{1}; // initialize i to 1
double d{2.0}; //…

James
- 221
- 1
- 5