# std::nothrow

> C++ Atlas: `cppatlas.com/w/cpp/memory/new/nothrow` | Source: https://en.cppreference.com/w/cpp/memory/new/nothrow | Domain: Memory management | Standards: C++11

std::nothrow_t is an empty class type used to disambiguate the overloads of throwing and non-throwing allocation functions.

## Official Specification Summary (cppreference)

```cpp
// Defined in header <new>
struct nothrow_t {}; // (1) (until C++11)
struct nothrow_t { explicit nothrow_t() = default; }; // (1) (since C++11)
extern const std::nothrow_t nothrow; // (2)
```

`std::nothrow_t` is an empty class type used to disambiguate the overloads of throwing and non-throwing allocation functions. `std::nothrow` is a constant of it.

### Example

```cpp
#include <iostream>
#include <new>

int main()
{
    try
    {
        while (true)
        {
            new int[100000000ul];   // throwing overload
        }
    }
    catch (const std::bad_alloc& e)
    {
        std::cout << e.what() << '\n';
    }

    while (true)
    {
        int* p = new(std::nothrow) int[100000000ul]; // non-throwing overload
        if (p == nullptr)
        {
            std::cout << "Allocation returned nullptr\n";
            break;
        }
    }
}
```

Output:

```text
std::bad_alloc
Allocation returned nullptr
```

### Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

| DR | Applied to | Behavior as published | Correct behavior |
| --- | --- | --- | --- |
| LWG 2510 | C++11 | the default constructor was non-explicit, which could lead to ambiguity | made explicit |

### See also

- [`operator new`](https://en.cppreference.com/w/cpp/memory/new/operator_new), [`operator new[]`](https://en.cppreference.com/w/cpp/memory/new/operator_new): allocation functions (function)

---
Attribution: content from https://en.cppreference.com/w/cpp/memory/new/nothrow (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.