Skip to main content
Code Review

Return to Revisions

4 of 5
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/

Allowing switching between operator[] and at() based on NDEBUG macro version

The problem:

std::vector and other containers have two functions for accessing / modifying their content: operator[] and at().

at() is meant for debugging, to catch out-of-bound bugs. However, at() is also quite a lot slower than operator[]. Both above facts would yearn for a way to switch between the use of at() and operator[] based on whether it is a debug build or a production build, especially if performance matters.

However, I can see no such easy way. Sadly standard containers don’t offer a flag or sth that would allow to do this.

I tried to write a simple wrapper that would switch between at() and operator[] based on whether or not NDEBUG is defined, in line with how the assert macro works.

Note: This is a different solution to the same problem as stated here: https://codereview.stackexchange.com/q/158078/78399 This solves the issue of 2d arrays. But I wanted to post two solutions, 'cuz I feared You could frown upon such a macro :(

The code:

// AOO = At Or Operator[]
#ifndef NDEBUG
 #define AOO(INDEX) .at(INDEX)
#else
 #define AOO(INDEX) [INDEX]
#endif

The use:

void testNormalUse()
{
 std::vector<int> vec(100);
 vec AOO(50) = 5;
 std::cout << vec AOO(50) << std::endl;
}
 
void testRequireConst()
{
 const std::vector<int> vec(100, 5);
 std::cout << vec AOO(50) << std::endl;
}
 
void testRvalueRef()
{
 std::vector<int>vec(100, 5);
 std::cout << std::move(vec) AOO(50) << std::endl;
}
 
void test2d()
{
 std::vector<std::vector<int>> vec(100, std::vector<int>(100, 5));
 std::cout << vec AOO(50) AOO(50) << std::endl;
}
 
int main() {
 testNormalUse();
 testRequireConst();
 testRvalueRef();
 test2d();
}

Ideone

Of course I know this is weird syntax. But the non-macro solution brings up pain with multidimensional arrays, a problem the macro version solves.

If only macros allowed punctuation, then we could make it look operator-like. But alas, they don't.

gaazkam
  • 581
  • 2
  • 14
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /