Should _GLIBCXX_DEBUG affect tr1/array?

Edward Rosten edward.rosten@gmail.com
Tue Jan 17 11:17:00 GMT 2012


On Mon, Jan 16, 2012 at 7:49 PM, Jonathan Wakely <jwakely.gcc@gmail.com> wrote:


> You can throw from a constexpr function, as long as the throw
> expression is part of the return statement.
>
> constexpr bool f(bool b) { return b ? b : throw false; }
>
> int main(int argc, char** argv)
> {
>    return f(argc > 1);
> }

How interesting! I didn't realist that. In that case, is the following OK?

It seems to fit the bill about being both constexpr and bound checking
with the libstdc++ standard error messages:

#define _GLIBCXX_DEBUG
#include <debug/debug.h>


template<int Size> struct Die
{
	int size() const
	{
		return Size;
	}

	Die(int n)
	{
		__glibcxx_check_subscript(n);
	}
};

template<int Size, class C> struct Array
{
	constexpr int operator[](int n) noexcept
	{
		return n>=Size?throw Die<Size>(n):0;
	}
};

int main()
{
	Array<10, int> a;

	a[11];
}
I think the main refinement would be not to be some conditional
compilation so that throw is never used in non-debug mode to allow for
better optimization.

-Ed



More information about the Libstdc++ mailing list