I'm trying to understand when implicit conversions happen and to detect them to improve the quality of my codebase.
I have enabled Wconversion and Wsign-conversion for this. However I came across a situation where the compiler doesn't give any error. Example:
#include <iostream>
#include <array>
int main()
{
std::array<int, 10> vec{};
std::cout << vec[1] << std::endl;
}
Compiling:
$ g++ --std=c++14 -Wall -Wextra -Werror -pedantic -Wsign-conversion -Wconversion test.cpp
$
Both the size of the array and the index to operator[] should be of type std::size_t (unsigned). However I'm passing signed literals and there seems to be no problem. I could even pass 1.0F to operator[] and the compiler would be fine.
If I create a signed variable for the index to operator[], however, then the compiler does warn about implicit conversion.
What's happening under the hood? Are implicit conversions happening when using literals? Why doesn't the compiler give an error? I'm using GCC 7.4 on Ubuntu 18.04.