Search code examples
c++vectorcharintnarrowing

Initialize vector <char> with int values


I want to initialize this char vector with this ASCII codes:

vector <char> a = { 201, 187, 200, 188, 205, 186 };

and I get this syntax error in all of the 6 characters:

Invalid narrowing convertion from "int" to "char": constant value does not fit in destination type

but when I initialize a single char variable, with the same ASCII codes:

char b = 201;

It works fine.

So I realized that in vectors, for some reason, char type can receive int values until 127. Starting 128 the syntax error appears.

This is different than in normal variables, when char type can receive any int values.

I tried declaring the vector as unsigned char, and the syntax error dissapears.

vector <unsigned char> a = { 201, 187, 200, 188, 205, 186 };

But still,

Why char type vectors cannot receive the same int data as char type variables?

I really would appreciate someone explaining me this behavior.


Solution

  • There are two things going on.

    1
    The first is the default range of values for the char type, which is implementation defined. In most major compilers the default is [-128,127]. (And, oddly, this is not the same as signed char, JSYK!)

    Both MSVC and GCC provide options to treat char as signed or unsigned. You can do that to fix the problem globally.

    Better, though, is not to assume char handles anything outside the range [0,127]. Use signed char or unsigned char to be specific.

    2
    The second is that you are using brace initialization, which requires that literal element values be checked for range fit.

    For your examples:

    std::vector <char> xs = { 201, 202 };  // compiler error
    
    char x { 201 };  // compiler error
    
    char x = 201;  // compiler warning
    

    If you don’t have your compiler’s error level cranked up (and you should) then the compiler will silently ignore that warning and assign the value, even though it is technically invalid.