This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Should _GLIBCXX_DEBUG affect tr1/array?


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]