Home > Enterprise >  Why text.size() returns std::size_t and not std::string::size_type
Why text.size() returns std::size_t and not std::string::size_type

Time:05-27

I am studying c and currently reading C Primer (5th ed). I am on the topic about std::string. The size member returns the length of the string. The book says (p. 88)

auto len = line.size(); // len has type string::size_type

But Visual Studio Code identifies len as having the type std::size_t. Why is it different?

CodePudding user response:

identifies len as having the type std::size_t. Why is it different?

From microsoft's basic_string documentation:

typedef typename allocator_type::size_type size_type;

Remarks

it's equivalent to allocator_type::size_type.

For type string, it's equivalent to size_t.

(emphasis mine)

Thus, as can be seen from the above quoted remark, for std::string, it is equivalent to size_t.

  • Related