1

I want to use the std::string(size_type count,CharT ch) with a big value for count. Reading https://en.cppreference.com/w/cpp/string/basic_string/basic_string, I could not find an exception definition for this constructor, in case it fails.

If it is correct, although there is not a noexcept clause in the constructor, how can I be sure that the string was created? Should I check if its size is not 0?

1
  • 3
    The basic_string template uses the allocator to allocate memory, and by default it's the std::allocator template, whose allocate member function can throw std::bad_alloc on failure. Commented Aug 25, 2019 at 14:53

1 Answer 1

5

Your link says it under Exceptions:

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.

Also std::string uses an allocator which means std::bad_alloc can be thrown aswell, if the allocator fails to allocate the requested amount of memory.

Sign up to request clarification or add additional context in comments.

2 Comments

I'd also mention std::bad_alloc.
@canellas Consider accepting this answer to remove your question from the list off unanswered questions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.