我如何知道是否实际创建了std :: string?

can*_*las 1 c++ out-of-memory stdstring

我想使用std::string(size_type count,CharT ch)具有较大价值的count。阅读https://en.cppreference.com/w/cpp/string/basic_string/basic_string,如果此构造函数失败,我将找不到异常定义。

如果它是正确的,尽管noexcept构造函数中没有子句,如何确定该字符串已创建?我应该检查它的大小是否不为0?

Tim*_*imo 5

您的链接在“ 例外”中说明了这一点:

如果构造的字符串的长度超过max_size(),则抛出std :: length_error(例如,如果(2)的count> max_size())。调用Allocator :: allocate可能会引发。

std::string使用分配器,这意味着std::bad_alloc如果分配器无法分配请求的内存量,也可以抛出该分配器。