narrowing Questions

3

Solved

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 impossi...
Virnelli asked 28/9, 2021 at 21:49

1

TypeScript 4.9's satisfies operator is super handy, allowing narrowly typed values that still match a wide definitions. type WideType = Record<string, number>; const narrowValues = { ...
Songwriter asked 30/5, 2023 at 17:47

1

I don't understand this: const VALUES = { name: "name", age: "age", address: "address", }; export function getVal(key: string) { if (key in VALUES) { // Element i...
High asked 1/11, 2022 at 9:52

1

Solved

I'm trying to create a custom type guard using an instanceof but strangely it isn't working as expected in the else clause This is an example with the related playground link: Playground Link class...
Siloa asked 2/11, 2022 at 10:29

1

Solved

I've come across an interesting issue, and I can't understand what's happening: /* I WANT 6 ELEMENTS */ int lvalue = 6; std::vector<int*> myvector { 6 }; /* WORKS FINE */ std::vector<int*...
Studious asked 5/6, 2022 at 5:30

2

Solved

If the members of a union type share a property, and the type of that property can be used to discriminate between those members, I should be able to narrow the type within an if clause using typeo...

1

Solved

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? Giv...
Rains asked 12/10, 2021 at 23:20

2

Solved

I would like to link 2 generics types in a function, and use narrowing for both types by checking one of them. What is the correct way to do this? type A = 'A'; type B = 'B'; type AB = A | B type...
Sucrose asked 23/8, 2021 at 18:11

1

Solved

Why does the Typescript compiler complain about the following code? type Foo = { a: string } type Bar = { b: number } type Baz = Foo | Bar; function f(x: Baz): number { if (x.a) { // property...
Collin asked 16/2, 2021 at 22:42

1

Solved

Consider the following program: template<typename T> constexpr int f() { T{}.i; // error if instantiated with [T = double] return 42; } constexpr void g(char); using U = decltype( g( {f&...
Littell asked 24/8, 2020 at 2:13

4

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 sa...

4

Solved

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 ...
Cystolith asked 17/10, 2018 at 14:54

3

Background: I'm writing a wrapper type like Either<A, B>, and I'd like return {some, args}; to work from a function returning Either<A, B> exactly when it'd work from a function returni...
Teodoor asked 17/11, 2017 at 4:9

1

Solved

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;...
Latimore asked 9/9, 2019 at 13:37

2

Solved

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 th...
Outskirts asked 17/10, 2018 at 21:8

1

Solved

Compiling this code using g++ -std=c++17 -Wall -pedantic main.cpp doesn't produce any warnings: #include <iostream> #include <stdlib.h> int main(int argc, char const *argv[]) { for (...
Oliveolivegreen asked 21/9, 2018 at 11:43

7

Solved

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 fo...
Mccaskill asked 6/2, 2013 at 22:30

4

Solved

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...

4

Solved

Consider the following piece of code: #include <cstdint> 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 mai...
Stooge asked 17/3, 2018 at 16:41

3

Solved

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<uint8_t, 4> bytes = ...
Incipient asked 3/10, 2017 at 13:32

1

Solved

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<M extends Mes...
Carreon asked 20/3, 2017 at 21:8

3

Solved

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, erro...
Neurath asked 25/12, 2016 at 14:0

0

Consider the following code: auto f() -> decltype(int{0.}, void()) { } int main() { f(); } It doesn't compile (as expected) with an error: narrowing conversion of '0.0' from 'double' to 'i...
Kavanagh asked 7/11, 2016 at 23:14

1

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. in...
Connective asked 19/4, 2016 at 11:18

2

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...
Agnate asked 28/7, 2015 at 19:9

© 2022 - 2024 — McMap. All rights reserved.