A type conversion where the destination type which cannot represent all values in the source type.
Questions tagged [narrowing]
167 questions
11
votes
1 answer
Is gcc wrong not diagnose narrowing conversions in non-type template arguments?
The following program compiles without errors or warning with gcc 4.8.1,
-Wall -std=c++11:
template
struct A{};
int main(){
A<1-2> a;
(void)a;
return 0;
}
clang 3.3 with the same options gives this error:
error: non-type…

Mike Kinghan
- 55,740
- 12
- 153
- 182
10
votes
1 answer
Bing Search API: Narrow by date
In it's current version, is it possible to use Bing's "Narrow By Date" feature when accessing it's API?
I cannot find any information about how to narrow the results such that it only shows results from the "past 24 hours" or "past week" (and so…

DRobinson
- 4,441
- 22
- 31
9
votes
1 answer
What are the consequences of ignoring narrowing conversions in C++0x
Since switching on the C++0x standard in g++, I've started seeing 'narrowing conversion' errors, particularly when converting from an 'int' to a 'short' although I understand the error covers a much broader swath of conversions.
Can anyone shed some…

Gearoid Murphy
- 11,834
- 17
- 68
- 86
9
votes
2 answers
Optional<> and return type narrowing
In Java < 8, returning "unsafe" objects (objects or null), I was able to specialize return type in subclass:
class A {}
class B extends A {}
interface Sup { A a(); /* returns A instance, or null */ }
interface Sub extends Sup { B a(); }
In Java 8,…

leventov
- 14,760
- 11
- 69
- 98
8
votes
2 answers
Types narrowing with generics
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 ComplexType = {value: T}
const f = (next:…

soe165
- 83
- 1
- 5
8
votes
3 answers
Why does the expression below characterize a narrowing conversion?
This expression can be found in the Example in §8.5.4/7 in the Standard (N3797)
unsigned int ui1 = {-1}; // error: narrows
Given §8.5.4/7 and its 4th bullet point:
A narrowing conversion is an implicit conversion:
from an integer type or unscoped…

Wake up Brazil
- 3,421
- 12
- 19
7
votes
2 answers
class cast exception in narrow a jndi reffrence in ejb
I am trying to write a simple stateless sesssion bean but I have problem with narrow reference I give in lookup time.
I got
class cast exeption
I use
eclipse IDE
my bean class
package codes;
import java.rmi.RemoteException;
import…

sara
- 73
- 1
- 1
- 3
7
votes
1 answer
Why can Typescript not figure out the type in my code?
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 'a' does not exist on type Bar!
return 0;
…

Jared Smith
- 19,721
- 5
- 45
- 83
7
votes
1 answer
Is a function definition required to be instantiated when there is no need to check for narrowing?
Consider the following program:
template
constexpr int f()
{
T{}.i; // error if instantiated with [T = double]
return 42;
}
constexpr void g(char);
using U = decltype( g( {f()} ) );
To my understanding, the last line…

cigien
- 57,834
- 11
- 73
- 112
7
votes
1 answer
Why doesn't C++ show a narrowing conversion error when casting a float to a char?
Compiling this code using g++ -std=c++17 -Wall -pedantic main.cpp doesn't produce any warnings:
#include
#include
int main(int argc, char const *argv[]) {
for (int i = 0; i < 100; ++i) {
float x = 300.0 + rand();
…

Kristopher Ives
- 5,838
- 7
- 42
- 67
7
votes
0 answers
Narrowing, unevaluated context and template function
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 'int' inside { }
Both GCC and clang agree on that.
Now…

skypjack
- 49,335
- 19
- 95
- 187
7
votes
1 answer
Create array of chars avoiding narrowing
I am writing a unit test checking some binary data against an expected array. The expected array in question is just some series of bytes, doesn't matter specifically:
char expected[] = {0x42, 0xde, 0xad, 0xbe, 0xef};
This compiled fine in C++, but…

Barry
- 286,269
- 29
- 621
- 977
7
votes
2 answers
Why does `bool b = 2` work well but `bool b = {2}` yield a warning of narrowing conversion?
Using the {} initializer in C++11 to initialize bool b = {2} yields the following warning message:
warning: narrowing conversion of ‘2’ from ‘int’ to ‘bool’ inside { } [-Wnarrowing]
However, using the old style bool b = 2 has no such problem. What…

tianz
- 2,155
- 2
- 19
- 28
6
votes
1 answer
How can I use instanceof inside a custom type guard in typescript?
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 Person {}
class Animal {}
const isPerson = (obj:…

limdev
- 706
- 1
- 5
- 18
6
votes
2 answers
Why doesn't 'typeof' narrow a union type when the type of a property is a discriminant?
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 typeof as a condition. But it doesn't work.
For example,…

hliu
- 1,015
- 8
- 16