So, given the following OP comment:
vec vector (5); not UB (is it?), and we tend to think that (5) and (5, S ()) should do the same. I could see that this mistake was made by accident rather easily.
The question boils down to the fact that:
vector<S> vec(5);
clearly defined, why:
std::vector<S> vec(5, S());
undefined behavior?
If we move on to the cppreference std :: vector :: vector section, we will see this in the first case (starting with C ++ 11) (my attention):
Creates a container with T instances installed by default. No copies .
and in the second case:
Creates a container with copies of elements with a value of value.
In the first case, elements will be built by default, and copies will not be made, and in the second case, copies will be made, and therefore we will end copying x to each element. Since the default constructor of S does not initialize x , it will have an undefined value, and therefore we have undefined behavior, since undefined behavior is called when creating an undefined value.
Since section C ++ 14 8.5 paragraph 12 says:
If an undefined value is obtained by evaluating, the behavior is undefined, except in the following cases:
with some exceptions in the case of an unsigned char, which in this case does not apply.
We know that x has an undefined value from the same paragraph, which reads:
When storing for an object with automatic or dynamic storage duration, the object has an undefined value, and if not, the object is initialized, this object retains an undefined value until this value is replaced