C++ Logo

std-proposals

Advanced search

Re: [std-proposals] noexcept(static)

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 17 Sep 2026 11:36:30 +0100
'sqrt' is a bad example because it has a wide contract -- you're allowed to
give it a negative number without inducing UB.

Let's talk about 'std::vector::operator[]' instead. If you give it an
invalid index, you get UB. Because of the Lakos rule, it's marked
'noexcept(false)'.

Consider if we write this:

    int &Monkey( vector<int> &v, size_t const n ) noexcept(static)
    {
        return v[n];
    }

Basically I want this to be compiled as follows:

    int &Monkey( vector<int> &v, size_t const n ) noexcept
    {
        static_assert( noexcept( v[n] ) );
        return v[n];
    }

The above will unfortunately fail to compile. So I think what we want is
something like:

    int &Monkey( vector<int> &v, size_t const n ) noexcept
    {
        static_assert( noexcept_if_unhardened( v[n] ) );
        return v[n];
    }

So then maybe we should have:

    value_type &vector::operator[](size_t) noexcept_if_hardened(false)
noexcept_if_unhardened(true);

Maybe.... ? Just mulling through a rumination of ideas here . . .

Received on 2026-09-17 10:36:33