[array] compile-time checked access

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

[array] compile-time checked access

Tobias Loew
Hi,

I was just rewriting some old code with hard coded indices for array access and though that compile-time checked access to array elements would be a nice thing.
Maybe there are smart compilers out there that emit warnings or errors for a hard coded out-of-bounds access, but my compiler does not.

So, code like

boost::array<int,2> test;
test[2] = 1;
test[-1] = 1;

compiles without warnings or erros.
Now, adding the following functions to array

        template<size_type i>
        reference at() 
        { 
            BOOST_STATIC_ASSERT( (i < N) );
            return elems[i]; 
        }

        template<size_type i>
        const_reference at() const
        { 
            BOOST_STATIC_ASSERT( (i < N) );
            return elems[i]; 
        }

would make it possible to write

boost::array<int,2> test;
test.at<2> = 1;
test.at<-1> = 1;

which results in the expected error messages.

Tobias
Reply | Threaded
Open this post in threaded view
|

Re: [array] compile-time checked access

TONGARI J
2012/11/5 Tobias Loew <[hidden email]>

> Hi,
>
> I was just rewriting some old code with hard coded indices for array access
> and though that compile-time checked access to array elements would be a
> nice thing.
> Maybe there are smart compilers out there that emit warnings or errors for
> a
> hard coded out-of-bounds access, but my compiler does not.
>
> So, code like
>
>
>
> compiles without warnings or erros.
> Now, adding the following functions to array
>
>
>
> would make it possible to write
>
>
>
> which results in the expected error messages.
>

I see only the blanks, any code there?

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [array] compile-time checked access

Tobias Loew
I used the "raw"-tag to quote the code. Here is it again without the tags:

...
So, code like

    boost::array<int,2> test;
    test[2] = 1;
    test[-1] = 1;


compiles without warnings or erros.
Now, adding the following functions to array

        template<size_type i>
        reference at()
        {
            BOOST_STATIC_ASSERT( (i < N) );
            return elems[i];
        }

        template<size_type i>
        const_reference at() const
        {
            BOOST_STATIC_ASSERT( (i < N) );
            return elems[i];
        }


would make it possible to write

    boost::array<int,2> test;
    test.at<2> = 1;
    test.at<-1> = 1;


which results in the expected error messages.
Reply | Threaded
Open this post in threaded view
|

Re: [array] compile-time checked access

Jens Auer-5
In reply to this post by Tobias Loew
Hi,

I cannot see any code but only newlines in your post, so I will not respond to the example. The sort of analysis you are looking for is done by static analysis tools (as far as possible, there are limitations). A classic one would be lint, but I don't know if they catch such errors. The documentation says that they do value tracking and some form of abstract interpretation. You should be able to find a lot of (commercial) tools with google if you search for static analysis c++. Klocwork and QA Systems are well-known vendors I know about.

The MS compiler also has some static analysis facilities and there exists a free static analyzer based on clang: http://clang-analyzer.llvm.org/.


Best regards,
  Jens

-----Urspr√ľngliche Nachricht-----
Von: [hidden email] [mailto:[hidden email]] Im Auftrag von Tobias Loew
Gesendet: Montag, 5. November 2012 09:51
An: [hidden email]
Betreff: [boost] [array] compile-time checked access

Hi,

I was just rewriting some old code with hard coded indices for array access and though that compile-time checked access to array elements would be a nice thing.
Maybe there are smart compilers out there that emit warnings or errors for a hard coded out-of-bounds access, but my compiler does not.

So, code like



compiles without warnings or erros.
Now, adding the following functions to array



would make it possible to write



which results in the expected error messages.

Tobias



--
View this message in context: http://boost.2283326.n4.nabble.com/array-compile-time-checked-access-tp4638135.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [array] compile-time checked access

Tobias Loew
That's right, there are a lot of tools to do static analysis. But why use a tool if you can do it within the language?

Jens Auer-5 wrote
The MS compiler also has some static analysis facilities and there exists a free static analyzer based on clang: http://clang-analyzer.llvm.org/.
I'm using MSVC 9 and

#include <boost/array.hpp>
void foo()
{
    boost::array<int,2> test;
    test[2] = 1;
}

is compiled without any warning even though /W4 is enabled.

Tobias
Reply | Threaded
Open this post in threaded view
|

Re: [array] compile-time checked access

Jeffrey Lee Hellrung, Jr.-2
In reply to this post by Tobias Loew
On Mon, Nov 5, 2012 at 6:02 AM, Tobias Loew <[hidden email]> wrote:

> I used the "raw"-tag to quote the code. Here is it again without the tags:
>
> ...
> So, code like
>
>     boost::array<int,2> test;
>     test[2] = 1;
>     test[-1] = 1;
>
>
> compiles without warnings or erros.
> Now, adding the following functions to array
>
>         template<size_type i>
>         reference at()
>         {
>             BOOST_STATIC_ASSERT( (i < N) );
>             return elems[i];
>         }
>
>         template<size_type i>
>         const_reference at() const
>         {
>             BOOST_STATIC_ASSERT( (i < N) );
>             return elems[i];
>         }
>
>
> would make it possible to write
>
>     boost::array<int,2> test;
>     test.at<2> = 1;
>     test.at<-1> = 1;
>
>
> which results in the expected error messages.
>

You can try to use boost::fusion::at_c<N>(test), but not sure if it
actually has a static assertion. See

http://www.boost.org/doc/libs/1_51_0/libs/fusion/doc/html/fusion/sequence/intrinsic/functions/at_c.html
http://www.boost.org/doc/libs/1_51_0/libs/fusion/doc/html/fusion/adapted/boost__array.html

- Jeff

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [array] compile-time checked access

Tobias Loew
Jeffrey Lee Hellrung, Jr.-2 wrote
On Mon, Nov 5, 2012 at 6:02 AM, Tobias Loew <[hidden email]> wrote:

> I used the "raw"-tag to quote the code. Here is it again without the tags:
>
> ...
> So, code like
>
>     boost::array<int,2> test;
>     test[2] = 1;
>     test[-1] = 1;
>
>
> compiles without warnings or erros.
> Now, adding the following functions to array
>
>         template<size_type i>
>         reference at()
>         {
>             BOOST_STATIC_ASSERT( (i < N) );
>             return elems[i];
>         }
>
>         template<size_type i>
>         const_reference at() const
>         {
>             BOOST_STATIC_ASSERT( (i < N) );
>             return elems[i];
>         }
>
>
> would make it possible to write
>
>     boost::array<int,2> test;
>     test.at<2> = 1;
>     test.at<-1> = 1;
>
>
> which results in the expected error messages.
>

You can try to use boost::fusion::at_c<N>(test), but not sure if it
actually has a static assertion. See

http://www.boost.org/doc/libs/1_51_0/libs/fusion/doc/html/fusion/sequence/intrinsic/functions/at_c.html
http://www.boost.org/doc/libs/1_51_0/libs/fusion/doc/html/fusion/adapted/boost__array.html

- Jeff

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
I tried it, but it doesn't static assert. Shouldn't it ?
Reply | Threaded
Open this post in threaded view
|

Re: [array] compile-time checked access

Mathias Gaunard-2
On 05/11/12 15:57, Tobias Loew wrote:

> I tried it, but it doesn't static assert. Shouldn't it ?

That's trivial to add.
You can add a ticket about it you want it.


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [array] compile-time checked access

Tobias Loew
Mathias Gaunard-2 wrote
On 05/11/12 15:57, Tobias Loew wrote:

> I tried it, but it doesn't static assert. Shouldn't it ?

That's trivial to add.
You can add a ticket about it you want it.


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Good idea. I've added a ticket #7651