标签: narrowing

使用int值初始化vector <char>

我想用这个ASCII码初始化这个char向量:

vector <char> a = { 201, 187, 200, 188, 205, 186 };
Run Code Online (Sandbox Code Playgroud)

我在所有6个字符中都会出现此语法错误:

从"int"到"char"的缩小转换无效:常量值不适合目标类型

但是当我使用相同的ASCII代码初始化单个char变量时:

char b = 201;
Run Code Online (Sandbox Code Playgroud)

它工作正常.

所以我意识到在向量中,由于某种原因,char类型可以接收int值直到127.从128开始出现语法错误.

这与普通变量不同,当char类型可以接收任何int值时.

我尝试将向量声明为unsigned char,语法错误消失.

vector <unsigned char> a = { 201, 187, 200, 188, 205, 186 };
Run Code Online (Sandbox Code Playgroud)

但是,

为什么char类型向量不能接收与char类型变量相同的int数据?

我真的很感激有人向我解释这种行为.

c++ int vector char narrowing

3
推荐指数
1
解决办法
2913
查看次数

为什么 TypeScript 不缩小数组类型?

为什么 TypeScript 不缩小数组类型?

function test(input: (string | number)[]): string[] {
  // The type of .map(...) reports that it returns string[].
  input = input.map(x => x.toString())
  // Type error: Type '(string | number)[]' is not assignable to type 'string[]'.
  return input
}
Run Code Online (Sandbox Code Playgroud)

解决方法并不依赖于通过立即使用或分配给新变量来缩小类型:

function test(input: (string | number)[]): string[] {
  return input.map(x => x.toString())
}

function test(input: (string | number)[]): string[] {
  const newInput = input.map(x => x.toString())
  return newInput
}
Run Code Online (Sandbox Code Playgroud)

我确实尝试过强制转换,但事后看来,这显然只适用于使用,例如return input as string[],并且不会缩小类型,因为.map(...)已经返回了正确缩小的类型。

必须执行这些解决方法对我来说感觉违反直觉。为什么 …

narrowing typescript

3
推荐指数
1
解决办法
6069
查看次数

为什么在添加 Short 时会出现从 int 到 Short 的缩小转换警告?(C++)

对于以下数组,我有一个与此类似的代码:

long int N = 424242424242; //random number
short int* spins = new short int spins[N];
std::fill(spins, spins+N, 1);

Run Code Online (Sandbox Code Playgroud)

现在假设由于某种原因我想将该数组的几个元素添加到一个名为 nn_sum 的短整型中:

short int nn_sum = spins[0] + spins[1];
Run Code Online (Sandbox Code Playgroud)

但是,当我在 CLion IDE 上执行此操作时,Clang-Tidy 将其标记为黄色并告诉我:

Clang-Tidy: Narrowing conversion from 'int' to signed type 'short' is implementation-defined
Run Code Online (Sandbox Code Playgroud)

为什么会发生这种情况?为什么会出现缩小呢?C++ 在添加短整型时是否会将它们转换为整数?如果是这样,为什么?我能做些什么来让它更好地工作吗?也许甚至完全放弃短裤?

请记住,我在应用程序的计算密集型部分中有这样的代码因此我希望使其尽可能高效。任何其他建议也将不胜感激。

c++ types type-conversion narrowing

3
推荐指数
1
解决办法
816
查看次数

使用 noexcept 的程序被 msvc 和 clang 接受但被 gcc 拒绝

我正在使用此处列出的书籍学习 C++ 。特别是最近学习了使用《C++ Primer》noexcept这本书。现在,为了进一步明确我对这个主题的概念并确认我已经正确理解了事情,我正在编写简单的程序。下面给出了一个这样的程序,它可以使用 MSVC 和 Clang 编译,但不能使用 GCC 编译。演示

void f() noexcept(5) //accepted by msvc but rejected by gcc
{

}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是哪个编译器就在这里(如果有的话)?

以下是一些主要编译器的结果:

编译器 C++版本 接受代码
海湾合作委员会 C++17
海湾合作委员会 C++20
C++17 是的
C++20
MSVC C++17 是的
MSVC C++20 是的

正如我们所看到的,该程序适用于某些编译器,但不适用于其他编译器。gcc 中的错误说: error: narrowing conversion of '5' from 'int' to 'bool'

c++ exception-specification language-lawyer narrowing noexcept

3
推荐指数
1
解决办法
117
查看次数

缩小从char到uint8_t的转换

使用C++ 11编译以下代码段(此处演示):

#include <stdint.h>

int main() {
    const uint8_t foo[] = {
        '\xf2'
    };
}
Run Code Online (Sandbox Code Playgroud)

将触发(至少在GCC 4.7)的警告,表明有转换时的收缩转换'\xf2'uint8_t.

为什么是这样?sizeof(char)总是1,应该是相同的sizeof(uint8_t),不应该吗?

请注意,当使用其他字符文字时'\x02',没有警告.

c++ narrowing c++11

2
推荐指数
1
解决办法
2569
查看次数

我可以在 TypeScript 中缩小范围吗?

我有一个实用程序函数来检查变量是否为空或未定义,如果通过检查,我希望 TypeScript 缩小输入变量的范围,例如:

public init(input?: string): void {
    function isSpecified(input: any): boolean {
        return (typeof input !== "undefined") && (input !== null);
    }

    if (isSpecified(input)) {
        let copiedString: string = input; // <-- Error; input is still 'string | undefined'
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的, TS 并没有消除字符串的可能性,undefined即使该函数在逻辑上是不可能的。有没有办法让这个函数调用缩小块input内的范围if

javascript narrowing typescript type-narrowing

2
推荐指数
1
解决办法
247
查看次数

C++标准对缩小从double到int的转换有什么保证?

C++标准对缩小从double到int类型的转换有什么保证?它与Q31328190中解释的Java相同:

c++ standards casting narrowing

1
推荐指数
1
解决办法
72
查看次数

从文字缩小不会引起警告

int a = 0; short b{a}; short c{0};

编译器给出了一个警告short b{a}.我能理解这一点,因为int它缩小了short.

但是,它没有给出警告short c{0},这对我来说很奇怪.我记得文字整数,0至少应该是这种类型int.因此,从缩小intshort发生在这里.为什么编译器没有发出警告?

c++ initialization language-lawyer narrowing list-initialization

1
推荐指数
1
解决办法
69
查看次数

G++ 引发编译错误而不是缩小转换的警告

我想获得编译错误而不是此代码的警告:

#include <iostream>

int main(int argc, char ** argv)
{
    float a = 1.3f;
    int b = 2.0 * a;

    std::cout << b << "\n";
}
Run Code Online (Sandbox Code Playgroud)

如果我编译它:

g++ test.cpp -o test
Run Code Online (Sandbox Code Playgroud)

我没有错误。

但是如果我编译相同的代码:

g++ test.cpp -o test -Wconversion
Run Code Online (Sandbox Code Playgroud)

我收到以下警告:

test.cpp: In function ‘int main(int, char**)’:
test.cpp:6:17: warning: conversion from ‘double’ to ‘int’ may change value [-Wfloat-conversion]
6 |     int b = 2.0 * a;
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种方式来获得编译错误,而不是警告仅适用于这种特定类型的警告。

Obs.1:-Werror可以使所有警告变成错误,但这不是我要找的

Obs.2:我正在使用 g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

c++ g++ narrowing

1
推荐指数
1
解决办法
51
查看次数