std::nothrow(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 functions
  • operator 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