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 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.


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