473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

std::vector range check

mk
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::operator[] 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
Jul 23 '05 #1
3 3631
"mk" <ka****@poczta.onet.pl> wrote in message
news:4d**************************@posting.google.c 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::operator[] 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
Jul 23 '05 #2
"mk" <ka****@poczta.onet.pl> wrote in message
news:4d**************************@posting.google.c 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::operator[] 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
Jul 23 '05 #3
>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
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: BCC | last post by:
Hi, A colleague has some code like this: class CMyObject { // Bunch of Member functions } class CMyObjectList: public std::vector<CMyObject> {
27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
2
by: Chris Thompson | last post by:
Hi I'm writing a p2p client for an existing protocol. I used a std::vector<char> as a buffer for messages read from the server. The message length is the first 4 bytes. The message code the...
6
by: Jason Heyes | last post by:
What is a good way of removing elements from std::vector so that the elements removed satisfy a predicate and end up stored in another std::vector. It seems as though the algorithm std::remove_if...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
3
by: Si | last post by:
How do I access the contents of an std:vector pointer (I mean access contents of the vector)? What is the correct notation for a pointer ( doesn't seem to work)?
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
6
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
7
by: digz | last post by:
template<typename _Tp, typename _Alloc> void vector<_Tp, _Alloc>:: _M_insert_aux(iterator __position, const _Tp& __x) { if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.