gpt4 book ai didi

C++ - 所有隐式转换的编译错误

转载 作者:行者123 更新时间:2023-11-27 23:47:53 28 4
gpt4 key购买 nike

是否有编译器标志在任何隐式转换(如 int32_t 到 uint32_t)的情况下记录警告/错误。

#include <cstdint>
#include <iostream>


using ::std::int32_t;
using ::std::uint32_t;


int main(int argc, char *argv[])
{

int32_t x = 9;

uint32_t i = x;

std::cout << " " << x << " " << i << std::flush << std::endl;

return 0;
}

c++ -std=c++11 -Wall -Wconversion -Wpedantic cast.cpp

我在编译期间没有遇到任何问题/警告/错误 - 有没有办法可以实现。

最佳答案

uint32_t i = x;

Signed int 类型到 unsigned int 类型的转换在 C++ 中有明确的定义。

(C++11,§4.7 ¶2)

If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type). [ Note: In a two’s complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). — end note ]

关于C++ - 所有隐式转换的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49104679/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com