Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::vector range check

Reply
Thread Tools

std::vector range check

 
 
mk
Guest
Posts: n/a
 
      04-01-2005
I will have 2 build types of my program: debug and release. In debug
build I want std::vector indexing operation (operator[] or at()) to do
a range-check. In release build I don't want it.

What is the easiest way to achieve that?

I currently have couple solutions, each one with drawbacks:

1. each time I change a build, search-and-replace in whole code
vector:perator[] with at() or vice-versa (this is not really a
solution of course)

2. derive a class from std::vector and override operator[] to do what
I need based on DEBUG macro.

This has a drawback of using non-standard class name (e.g. Vector)
that can potentially confuse future maintainers of the program.
Additionally, operator[] is not virtual, so overriding it is not
entirely safe, as the old one can be invoked accidentally under some
circumstances.

cheers,
Marcin Kalicinski
 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      04-01-2005
"mk" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om
> I will have 2 build types of my program: debug and release. In debug
> build I want std::vector indexing operation (operator[] or at()) to do
> a range-check. In release build I don't want it.
>
> What is the easiest way to achieve that?
>
> I currently have couple solutions, each one with drawbacks:
>
> 1. each time I change a build, search-and-replace in whole code
> vector:perator[] with at() or vice-versa (this is not really a
> solution of course)
>
> 2. derive a class from std::vector and override operator[] to do what
> I need based on DEBUG macro.
>
> This has a drawback of using non-standard class name (e.g. Vector)
> that can potentially confuse future maintainers of the program.
> Additionally, operator[] is not virtual, so overriding it is not
> entirely safe, as the old one can be invoked accidentally under some
> circumstances.
>
> cheers,
> Marcin Kalicinski



How about:

#ifdef _DEBUG
#define at(x) at(x)
#else
#define at(x) operator[](x)
#endif


--
John Carson
 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      04-01-2005
"mk" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
:I will have 2 build types of my program: debug and release. In debug
: build I want std::vector indexing operation (operator[] or at()) to do
: a range-check. In release build I don't want it.
:
: What is the easiest way to achieve that?
Use a non-member function that does what you want.
Something like [just typing on-the-fly]:
template<class C>
C::reference at(C& container, C::size_type index)
{
#ifdef NDEBUG
return container[index];
#else
return container.at(index);
#endif
}
// and a const version as well...

: I currently have couple solutions, each one with drawbacks:
:
: 1. each time I change a build, search-and-replace in whole code
: vector:perator[] with at() or vice-versa (this is not really a
: solution of course)
Not a reasonable option.

: 2. derive a class from std::vector and override operator[] to do what
: I need based on DEBUG macro.
Not a good idea either. Only create a new class when you have
new data members or new invariants to enforce.



Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


 
Reply With Quote
 
Stephen Howe
Guest
Posts: n/a
 
      04-01-2005
>I will have 2 build types of my program: debug and release. In debug
> build I want std::vector indexing operation (operator[] or at()) to do
> a range-check. In release build I don't want it.
>
> What is the easiest way to achieve that?


Preprocessor Macros are frowned on as a whole but I use the following for
vector and deque:

#if defined(_DEBUG)
#define AT(x) at(x)
#else
#define AT(x) operator[](x)
#endif

and then have something like

std::vector<int> v;
// later
int i = v.AT(0);

You can still use [] if you know you have checked the size() (or .at() if
not)

Stephen Howe


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I transform source range to destination range that is thesame as source? Lambda C++ 2 07-16-2008 05:18 PM
'ArgumentError: bad value for range' for range of Times David Bird Ruby 1 06-23-2008 12:12 PM
range() is not the best way to check range? Summercoolness@gmail.com Python 46 07-25-2006 08:10 PM
Scene range vs dynamic range Robert Feinman Digital Photography 2 07-04-2005 09:30 PM
Range does not take an Range object. Tomoyuki Kosimizu Ruby 3 11-27-2003 12:42 AM



Advertisments