Questions tagged [nothrow]

25 questions
34
votes
9 answers

When should std::nothrow be used?

What is the ideal usage of std::nothrow?
blitzkriegz
  • 9,258
  • 18
  • 60
  • 71
18
votes
2 answers

How to combine std::make_shared and new(std::nothrow)

C++'s new has an option to return a null pointer instead of throwing a bad_alloc exception when an allocation failed. Foo * pf = new(std::nothrow) Foo(1, 2, 3); (Yes, I understand this only prevents the new from throwing a bad_alloc; it doesn't…
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
15
votes
1 answer

Do std::make_shared and std::make_unique have a "nothrow" version?

For the new operator, we have the std::nothrow version: std::unique_ptr p = new(std::nothrow) T(); Do we have something like this for the std::make_shared or std::make_unique?
jack sparow
  • 339
  • 3
  • 9
7
votes
3 answers

Operator new with nothrow option still throws exception

There is such code: #include int main(){ for(;;){ int* ptr = new (std::nothrow) int; if(ptr == 0){ std::cout << 0 << std::endl; break; } } std::cin.get(); return…
scdmb
  • 15,091
  • 21
  • 85
  • 128
5
votes
2 answers

Why is std::pair not nothrow constructible?

The following code: #include #include #include #include using namespace std; struct Foo { std::string s; int i; }; int main() { cout << boolalpha << is_nothrow_constructible::value << endl; …
Nick
  • 10,309
  • 21
  • 97
  • 201
5
votes
4 answers

Placement new and exceptions

The "placement new" operator is declared as such: void* operator new (std::size_t size, void* ptr) noexcept; But while it doesn't involve any actual allocation so bad allocation exceptions are eliminated, it is still possible that the pointer…
user3735658
4
votes
3 answers

Is std::string's default constructor no-throw?

Can std::string s; throw under any circumstances? Is this regulated by the standard (interested in C++03, in case there are differences)?
Irfy
  • 9,323
  • 1
  • 45
  • 67
3
votes
1 answer

Why `std:variant`'s `operator=(T&& t)`'s noexcept spec doesn't depend on inner types's destructor's noexcept spec?

Long title: Why std:variant's operator=(T&& t)'s noexcept specification doesn't depend on inner types's destructor's noexcept specification? I can see on cppreference that template variant& operator=(T&& t) noexcept(/* see below…
Mate059
  • 103
  • 5
3
votes
2 answers

C++ Memory allocation with operator new: What are the methods of detecting and processing allocation errors?

In previous programs I have used the following code to check for memory allocation failures, usually without thinking about alternatives: int* p_int = new int[10]; if(!p_int) { // We failed, so exit return EXIT_FAILURE; } This method is…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
3
votes
2 answers

How to fail a constructor with new(std::nothrow)?

Consider the following code: #include #include #include void * operator new(size_t size) { void *res; if (size == 1) { res = NULL; } else { res = malloc(size); } fprintf(stderr,…
Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
3
votes
2 answers

What is the use of std::nothrow and std::new_handler in standard header file

I came across a small standard header file . I have probably not seen its direct use before. Here is the g++ version for those who are interested. Below part is of my interest: struct nothrow_t { }; extern const nothrow_t nothrow; /** If…
iammilind
  • 68,093
  • 33
  • 169
  • 336
2
votes
1 answer

Does specifying a constructor as noexcept implicitly result in the nothrow version of the new operator being used in C++?

If I specify a constructor as noexcept, does the nothrow version of the new operator implicitly get used when the object is dynamically instantiated? For example: class Something { public: Something() noexcept; }; ... Something *s = new…
Billy
  • 5,179
  • 2
  • 27
  • 53
2
votes
1 answer

new(std::nothrow) int[n] throws an exception

#include #include int main() { int n = -1; try { int *p = new(std::nothrow) int[n]; if(!p) std::cout << "new expression returned nullptr\n"; } catch(const std::bad_array_new_length&…
Lassie
  • 853
  • 8
  • 18
2
votes
1 answer

what to do upon failure of memory allocation new (nothrow) in c++ on linux

under no-exception context, I have seen several posts saying Thing* t = new(std::nothrow) Thing; // returns NULL on failure if (!t) { // allocation failure } e.g. How to check memory allocation failures with new operator? How to find out the…
pepero
  • 7,095
  • 7
  • 41
  • 72
2
votes
1 answer

How to specify nothrow exception specifier for destructor?

I am trying to specify that a function is nothrow whenever the destructor of Foo doesn't throw. I can do this by using the type trait std::is_nothrow_destructible<>. How can I do this directly? I've tried the following, but it doesn't compile if I…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
1
2