我怎么知道 std::string 是否真的创建了?

How can I know if a std::string was actually created?

我想使用 std::string(size_type count,CharT ch)count 的值很大。阅读 https://en.cppreference.com/w/cpp/string/basic_string/basic_string,我找不到此构造函数的异常定义,以防它失败。

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

你的link在例外下说:

Throws std::length_error if the length of the constructed string would exceed max_size() (for example, if count > max_size() for (2)). Calls to Allocator::allocate may throw.

另外 std::string 使用分配器,这意味着如果分配器未能分配请求的内存量,也会抛出 std::bad_alloc