Is there any use to using std :: string :: size_type over size_t?

I am looking through another developer code that contains the following:

std::string name;
...
std::string::size_type colon = name.find(":");

I would argue that the use of size_tit would be easier to understand and is as safe as the STL standard states that std::stringhave std::basic_string<char, std::allocator>, and std::allocator::size_type- size_t.

He wants to make sure that the STL standard cannot ever change to invalidate this assumption; if the standard can change, then it size_typewill be safer than size_t.

Could this happen? Is there any other reason to use size_type, not size_t?

+3
source share
2 answers

size_type , . , std::string, size_t.

+8

, - auto, , :

auto colon = name.find(":");

, , .

, struct , , . :

struct StoreStringIndex {
    decltype(declval<std::string>().length()) length;
};

std::string::size_type. , , size_type, .. auto.

+6
source

All Articles