我不明白为什么我会收到有关有符号和无符号之间转换的警告,是我的编译器错了吗?
I don't understand why I get this warning about conversion between signed and unsigned, is my compiler wrong?
我有这个代码:
#include <cstdint>
#include <deque>
#include <iostream>
int main()
{
std::deque<uint8_t> receivedBytes;
int nbExpectedBytes = 1;
if (receivedBytes.size() >= static_cast<size_t>(nbExpectedBytes))
{
std::cout << "here" << std::endl;
}
return 0;
}
使用 -Wsign-conversion,这在我的 linux 笔记本电脑上编译时没有警告,但在嵌入式 linux 上它意味着 运行 我收到以下警告:
temp.cpp: In function ‘int main()’: temp.cpp:10:33: warning:
conversion to ‘std::deque::size_type {aka long unsigned
int}’ from ‘int’ may change the sign of the result [-Wsign-conversion]
if (receivedBytes.size() >= static_cast<size_t>(nbExpectedBytes))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
就是没看懂:
- 我的 linux 笔记本电脑和嵌入式 linux 都启用了 -Wsign-conversion,那么为什么我只在嵌入式 linux 上收到警告?
- 我明确地从
int
转换为 size_t
(这不应该产生警告,因为转换是显式的),然后比较 size_t
和 std::deque<unsigned char>::size_type
],那么触发警告的从有符号到无符号的隐式转换在哪里??!
我不禁认为嵌入式linux上的编译器在这里是错误的。我错过了什么吗?
编辑:在我的 linux 笔记本电脑上,我使用的是 g++ 9.3.0 版,而在嵌入式 linux 上,我使用的是 g++ 6.3.0 版(可能不是通常的二进制文件,因为它是 ARM64 架构)
这无疑是嵌入式编译器中的一个bug/error。将 static_cast
与 >=
比较分开可以消除警告,从在 Compiler Explorer 上测试以下代码可以看出,选择了 ARM64 gcc 6.3.0 (linux):
#include <deque>
#include <cstddef>
#include <cstdint>
int main()
{
std::deque<uint8_t> receivedBytes;
int nbExpectedBytes = 1;
// Warning generated ...
while (receivedBytes.size() >= static_cast<size_t>(nbExpectedBytes))
{
break;
}
// Warning NOT generated ...
size_t blob = static_cast<size_t>(nbExpectedBytes);
while (receivedBytes.size() >= blob)
{
break;
}
return 0;
}
此外,当更改为(32 位)ARM gcc 6.3.0 (linux) 编译器时,警告也会消失。
我有这个代码:
#include <cstdint>
#include <deque>
#include <iostream>
int main()
{
std::deque<uint8_t> receivedBytes;
int nbExpectedBytes = 1;
if (receivedBytes.size() >= static_cast<size_t>(nbExpectedBytes))
{
std::cout << "here" << std::endl;
}
return 0;
}
使用 -Wsign-conversion,这在我的 linux 笔记本电脑上编译时没有警告,但在嵌入式 linux 上它意味着 运行 我收到以下警告:
temp.cpp: In function ‘int main()’: temp.cpp:10:33: warning: conversion to ‘std::deque::size_type {aka long unsigned int}’ from ‘int’ may change the sign of the result [-Wsign-conversion]
if (receivedBytes.size() >= static_cast<size_t>(nbExpectedBytes)) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
就是没看懂:
- 我的 linux 笔记本电脑和嵌入式 linux 都启用了 -Wsign-conversion,那么为什么我只在嵌入式 linux 上收到警告?
- 我明确地从
int
转换为size_t
(这不应该产生警告,因为转换是显式的),然后比较size_t
和std::deque<unsigned char>::size_type
],那么触发警告的从有符号到无符号的隐式转换在哪里??!
我不禁认为嵌入式linux上的编译器在这里是错误的。我错过了什么吗?
编辑:在我的 linux 笔记本电脑上,我使用的是 g++ 9.3.0 版,而在嵌入式 linux 上,我使用的是 g++ 6.3.0 版(可能不是通常的二进制文件,因为它是 ARM64 架构)
这无疑是嵌入式编译器中的一个bug/error。将 static_cast
与 >=
比较分开可以消除警告,从在 Compiler Explorer 上测试以下代码可以看出,选择了 ARM64 gcc 6.3.0 (linux):
#include <deque>
#include <cstddef>
#include <cstdint>
int main()
{
std::deque<uint8_t> receivedBytes;
int nbExpectedBytes = 1;
// Warning generated ...
while (receivedBytes.size() >= static_cast<size_t>(nbExpectedBytes))
{
break;
}
// Warning NOT generated ...
size_t blob = static_cast<size_t>(nbExpectedBytes);
while (receivedBytes.size() >= blob)
{
break;
}
return 0;
}
此外,当更改为(32 位)ARM gcc 6.3.0 (linux) 编译器时,警告也会消失。