[Development] QPair vs std::pair difference with narrowing conversion

Elvis Stansvik elvstone at gmail.com
Sat Jun 3 14:36:33 CEST 2023


Hi all,

I was going through some legacy code and came across a behavioral
difference between QPair and std::pair:

estan at edison:~$ cat test.cpp
#include <QPair>
#include <utility>

int main(void) {
   int i = 1;
   QPair<unsigned, unsigned>{i, i};
   std::pair<unsigned, unsigned>{i, i};
   return 0;
}
estan at edison:~$ g++ -isystem /usr/include/x86_64-linux-gnu/qt5
-isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -Wall -fPIC -c
test.cpp
test.cpp: In function ‘int main()’:
test.cpp:6:31: warning: narrowing conversion of ‘i’ from ‘int’ to
‘unsigned int’ [-Wnarrowing]
   6 |     QPair<unsigned, unsigned>{i, i};
     |                               ^
test.cpp:6:34: warning: narrowing conversion of ‘i’ from ‘int’ to
‘unsigned int’ [-Wnarrowing]
   6 |     QPair<unsigned, unsigned>{i, i};
     |                                  ^
estan at edison:~$

Just curious if anyone know why what difference between QPair and
std::pair makes the narrowing conversion warning pop up in the QPair
case and not the std::pair case?

Note the warning only appears when using aggregate initialization with { }.

Using GCC 11.3.0 and Qt 5.15.3.

Cheers,
Elvis


More information about the Development mailing list