Initializing a vector from another type of container iterator

Providing the following code:

#include <iterator>
#include <vector>

int main()
{
    char arr[3] = { 1,2,3 };
    std::vector<char> vec = { 1,2,3 };

    std::vector<int> vec_one(std::begin(arr), std::end(arr));
    std::vector<int> vec_two(vec.begin(), vec.end());
}

Are the initialization for vec_oneand vec_twoundefined, the implementation defined or defined in accordance with the norms of normal type conversion?
What to do if types charand intchange places?

+4
source share
2 answers

, , char int ( ) int char, : int , char, char - signed ( undefined) , char unsigned.

+5

, . int , char, , , , 16 . , sizeof(char) < sizeof(int) , int char. sizeof(char) == sizeof (int) and char is an alias to unsigned char then you could overflow the int`, undefined.

undefined. char signed char sizeof(int) > sizeof(char), int char, undefined. char unsigned char, undefined.

+4

Source: https://habr.com/ru/post/1680304/


All Articles