Boost logo

Boost :

Subject: [boost] Boost.Exception and constexpr
From: Marshall Clow (mclow.lists_at_[hidden])
Date: 2013-01-09 21:23:39

I was playing around with Boost.Array tonight, seeing if I could make the signatures more closely match std::array.

Specifically, I wanted to add 'constexpr' to "operator [] (size_t) const" and "at (size_t) const"as proposed in

But I ran into a problem.
Here are the implementations:

        const_reference operator[](size_type i) const
            BOOST_ASSERT_MSG( i < N, "out of range" );
            return elems[i];

        const_reference at(size_type i) const { rangecheck(i); return elems[i]; }

        static void rangecheck (size_type i) {
            if (i >= size()) {
                std::out_of_range e("array<>: index out of range");

BOOST_ASSERT does not fit into the constexpr world at all, and neither does rangecheck.

I could rewrite rangecheck so it could be constexpr, but only (I believe) by throwing the exception directly, rather than using boost::throw_exception.

        static void rangecheck (size_type i) {
            if (i >= size())
                throw std::out_of_range e("array<>: index out of range");

or even just:
        BOOST_CONSTEXPR const_reference at(size_type i) const {
                return i >= size () ? throw std::out_of_range e("array<>: index out of range") : elems[i];

Has anyone put any effort into making Boost.Exception (and BOOST_ASSERT) work with constexpr?

-- Marshall

Marshall Clow Idio Software <mailto:mclow.lists_at_[hidden]>

A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait).
        -- Yu Suzuki

Boost list run by bdawes at, gregod at, cpdaniel at, john at