我想使用std::string(size_type count,CharT ch)
,count
的值很大。读书https://en.cppreference.com/w/cpp/string/basic_string/basic_string ,我找不到此构造函数的异常定义,以防它失败。
如果是正确的,虽然构造函数中没有noexcept
子句,但我如何确定字符串被创建了呢?我应该检查它的大小是否不为 0?
最佳答案
您的链接在异常(exception)下说明:
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
。
关于c++ - 我如何知道是否实际创建了 std::string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57647083/