Should _GLIBCXX_DEBUG affect tr1/array?
Jonathan Wakely
jwakely.gcc@gmail.com
Tue Jan 17 12:24:00 GMT 2012
On 17 January 2012 11:17, Edward Rosten wrote:
>
> 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;
> }
I'm surprised that you can throw a non-literal type (i.e. one without
a constexpr constructor) from a constexpr function, but apparently
it's OK.
> };
>
> 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.
Yes, and so the comparison isn't done except in debug mode.
More information about the Libstdc++
mailing list