Это невозможно в реализации msvc2017 basic_string , поскольку при создании пустого std::string
будет выделено как минимум 15 символов.
Подробнее:
std::string.capacity()
возвращает _Myres
, который представляет текущую длину выделенного хранилища
// Implementation of std::string.capacity()
_NODISCARD size_type capacity() const noexcept
{ // return current length of allocated storage
return (this->_Get_data()._Myres);
}
// Construct an empty string would call _Tidy_init() method
basic_string() _NOEXCEPT_COND(is_nothrow_default_constructible_v<_Alty>)
: _Mybase()
{ // construct empty string
_Tidy_init();
}
// Moreover, initialize _Myres by enum constant value
void _Tidy_init()
{ // initialize basic_string data members
auto& _My_data = this->_Get_data();
_My_data._Mysize = 0;
_My_data._Myres = this->_BUF_SIZE - 1;
// the _Traits::assign is last so the codegen doesn't think the char
// write can alias this
_Traits::assign(_My_data._Bx._Buf[0], _Elem());
}
// _BUF_SIZE stands for literal number 16 (std::cout<<std::string::_BUF_SIZE;)
enum
{ // length of internal buffer, [1, 16]
_BUF_SIZE = 16 / sizeof (value_type) < 1 ? 1
: 16 / sizeof (value_type)};