std::nothrow(3)
π std::nothrow(3) β C++ Standard Library
okay so bestie, real talk: this is the std::nothrow page from the C++ Standard Library, and it's honestly shorter than my attention span but it's iconic in its own lowkey way. let's get into it π«Ά
β¨ NAME β¨
std::nothrow β literally just std::nothrow. that's the name. she's self-titled, main character energy, no notes π
πΈ SYNOPSIS πΈ
first things first: this whole situation lives in the header <new>. don't forget to include it or nothing works and you'll be sad π
here's the actual declaration, verbatim, no cap:
struct nothrow_t {};
(until C++11)
struct nothrow_t { explicit nothrow_t() = default; }; (1)
(since C++11)
extern const std::nothrow_t nothrow; (2)
so what's the tea? std::nothrow_t is an empty class type β yes, empty, she's minimal, she's chic β¨ β and her whole job is to disambiguate the overloads of throwing and non-throwing allocation functions. like, she's the little name tag that tells the compiler which new you meant. and std::nothrow is a constant of it. a whole const object. living her truth. π
π¦ EXAMPLE π¦
bestie run this code, it's giving educational AND dramatic:
// Run this code
#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;
}
}
}
the vibe check here: the first loop is the throwing overload, so when the memory runs out it yeets a std::bad_alloc at you and you catch it. the second loop is the non-throwing overload β it doesn't scream, it just quietly hands you nullptr and you check for it like a responsible adult π§
β OUTPUT β
std::bad_alloc
Allocation returned nullptr
see? one of them yelled, one of them just sighed. both valid. π
π₯ DEFECT REPORTS π₯
okay so the committee had to retroactively fix stuff β these behavior-changing defect reports got applied to already-published C++ standards. history being rewritten in real time, we love to see it π
- DR:
LWG 2510 - Applied to: C++11
- Behavior as published: the default constructor was non-explicit, which could lead to ambiguity
- Correct behavior: made explicit
so yeah, the default constructor got promoted to explicit because ambiguity is not the vibe. periodt. π
π SEE ALSO π
operator newβ allocation functionsoperator new[](function)
go read those next, i believe in you π«Άβ¨
that's the whole page, bestie β short, sweet, and fully documented. slay. π
source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::nothrow(3) β mwah, drink some water, go touch grass, and may all your allocations return exactly what you expected ππ¦
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms