Questions tagged [narrowing]

A type conversion where the destination type which cannot represent all values in the source type.

167 questions
6
votes
3 answers

Is it possible to avoid static_cast in initializer list?

In my code base I often initialize array or vector if bytes using the following the syntax: uint16_t foo = 0xAB, bar = 0xCD // bytes = { 0xA, 0xB, 0xC, 0xD } std::array bytes = {{ foo >> 8, foo & 0x00FF, bar >> 8, bar &…
AMDG
  • 955
  • 1
  • 9
  • 25
6
votes
1 answer

TypeScript type inference/narrowing challenge

I'm currently trying to improve the types on some existing code. My code looks roughly like this: /* dispatcher.ts */ interface Message { messageType: string; } class Dispatcher { on< MessageType extends…
Pat
  • 138
  • 4
6
votes
0 answers

narrowing conversion from 'int' to 'double' and array initialization

The following int i = 0; double d{i}; gives an error (in clang) or warning (in gcc) of a narrowing conversion from 'int' to 'double'. I found it amazing that this is really narrowing, at least until I saw narrowing conversion from unsigned to…
iavr
  • 7,547
  • 1
  • 18
  • 53
5
votes
1 answer

Why "key in myObject" does not narrow the type if key is of type string?

I don't understand this: const VALUES = { name: "name", age: "age", address: "address", }; export function getVal(key: string) { if (key in VALUES) { // Element implicitly has an 'any' type because expression of type 'string' can't be used…
luismartinezs
  • 199
  • 1
  • 2
  • 11
5
votes
4 answers

Difference between narrowing and truncation in C++?

I've been going through a book (C++ Programming Language Stroustrup 4th ed). An example given in a section related to Initialization as below: void f(double d, int i) { int a{ d }; // error : possible truncation char b{ i }; // error…
pasha
  • 2,035
  • 20
  • 34
5
votes
4 answers

Hidden narrowing conversion from int to uint8_t

Consider the following piece of code: #include class A { public: explicit A(uint8_t p_a){ m_a = p_a; }; uint8_t get_a() const {return m_a;} private: uint8_t m_a; }; int main() { A a {0x21U}; A aa{0x55U}; …
BobMorane
  • 3,870
  • 3
  • 20
  • 42
5
votes
3 answers

How can I detect whether a type is list-initializable?

Background: I'm writing a wrapper type like Either, and I'd like return {some, args}; to work from a function returning Either exactly when it'd work from a function returning A or B. However, I also want to detect when both A and B…
jtbandes
  • 115,675
  • 35
  • 233
  • 266
5
votes
1 answer

Strange behavior of narrowing in context of initializer lists

Does someone knows why this compiles without warnings int main() { const int i = 1024; std::initializer_list i_l = { i }; // no warning return 0; } but does not int main() { const int i = pow(2,10); std::initializer_list
abraham_hilbert
  • 2,221
  • 1
  • 13
  • 30
5
votes
2 answers

G++ 4.5 Bug: No diagnostic for narrowing in initializer list

I tried the following code: int main() { int x {23.22}; } which includes an initialization that requires narrowing, but the code compiles fine without any error or warning. On the other hand, the following code gives error: int main() { int…
Saurabh Manchanda
  • 1,115
  • 1
  • 9
  • 21
5
votes
4 answers

C++11: "narrowing conversion inside { }" with modulus

I try to compile the following code with gcc and C++11 enabled: unsigned int id = 100; unsigned char array[] = { id % 3, id % 5 }; I get these warnings: narrowing conversion of ‘(id % 3u)’ from ‘unsigned int’ to ‘unsigned char’ inside { }…
m.s.
  • 16,063
  • 7
  • 53
  • 88
4
votes
1 answer

Get Narrow Type from Object of Methods

I want to get a Narrowing type like this: type Expected = { method: 'firstNamespace/firstMethod' payload: [string] } | { method: 'firstNamespace/secondMethod' payload: [number, number] } | { method: 'secondNamespace/firstMethod' …
Katarn
  • 43
  • 3
4
votes
1 answer

How to get clang to warn about very simple narrowing

If I am using clang tools, what is the recommended way to get clang or some part of the clang toolchain to tell me that e.g. passing an int to a function that takes a short might be a bad idea? Given this very simple program static short sus =…
Chip Grandits
  • 317
  • 1
  • 9
4
votes
1 answer

Why doesn't braced initialization throw a narrowing error when converting from double to float?

Two things every C++ tutorial mentions early on: Braced initialization is generally superior when possible, because it will throw an error during a narrowing conversion such as int narrow{1.7}; // error: narrowing conversion of '1.7e+0' from…
JMA
  • 334
  • 1
  • 9
4
votes
1 answer

Narrowing conversion from char to double

Why there is a warning narrowing conversion from char to double I know already that for const char there will be no warning. There are a lot of answers about that. But I would like to know, why for non-const char there is a "might-narrow"…
Wichr
  • 43
  • 3
4
votes
2 answers

P0960, Is there any kind of mechanic to detect if there are narrowing in the new aggregates init with ()s in c++20?

With P0960 "Allow initializing aggregates from a parenthesized list of values", you can do aggregates init with ()s also. However, this initialization allows narrowing while {}s doesn't. #include #include struct Foo { int x,…
sandthorn
  • 2,770
  • 1
  • 15
  • 59
1 2
3
11 12