I have a code with very simple logical arithmetic that involves values returned from std::atomic_bool
.
#include <iostream>
#include <atomic>
int main() {
uint16_t v1 = 0x1122;
uint16_t v2 = 0xaaff;
std::atomic_bool flag1(false);
uint16_t r1 = v1 | v2;
std::cout << std::hex << r1 << std::endl;
uint16_t r2 = static_cast<uint16_t>(flag1.load()) | static_cast<uint16_t>(0xaaff);
std::cout << std::hex << r2 << std::endl;
std::cout << __VERSION__ << std::endl;
}
Code example is here. Compile line: g++ -std=c++17 -O3 -Wall -pedantic -Wconversion -pthread main.cpp && ./a.out
.
Based on the STD API, load()
should return the underlined type stored in the atomic. So the flag1.load()
should be returning bool
. However, the compiler send a warning that it is asked to convert an int
to uint16_t
:
main.cpp:13:55: warning: conversion from 'int' to 'uint16_t' {aka 'short unsigned int'} may change value [-Wconversion]
uint16_t r2 = static_cast<uint16_t>(flag1.load()) | static_cast<uint16_t>(0xaaff);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Where exactly does it do this conversion? Both sides of the |
are converted to uint16_t
. Why is it still printing a warning?