I want to know - does C++ do implicit cast when we initialize unsigned size_t with some value? Like this:
size_t value = 100;
And does it make sense to add 'u' literal to the value to prevent this cast, like this?
size_t value = 100u;
I want to know - does C++ do implicit cast when we initialize unsigned size_t with some value? Like this:
size_t value = 100;
And does it make sense to add 'u' literal to the value to prevent this cast, like this?
size_t value = 100u;
Copyright © 2021 Jogjafile Inc.
Yes.
std::size_tis an (alias of an) integer type. An integer type can be implicitly converted to all other integer types.There is still likely an implicit conversion with the
uliteral suffix sincestd::size_tis not necessarily (nor typically)unsigned int. It may be for exampleunsigned long intorunsigned long long int. There is a standard proposal to add integer literal for thestd::size_talias, but there doesn't exist one for now.Using a matching literal doesn't matter much in this example, as long as the type of the literal can represent the value in question, and as long as the literal value doesn't exceed the bounds of the initialised type. Even the smallest integer types can represent 100. The choice is largely a matter of taste.
Note that "implicit cast" is a contradiction in terms. Cast is an explicit conversion.