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 at()
is meant for debugging, to catch out-of-bound bugs. However, at()
is also quite a lot slower than operator[]
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();
}
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.
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();
}
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.
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();
}
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.
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: http://codereview.stackexchange.com/q/158078/78399 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();
}
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.
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: http://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();
}
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.
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();
}
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.
Allowing switching between `operator[]`operator[] and `atat()` based on `NDEBUG` Macro VersionNDEBUG macro version
http://ideone.com/8QPfIxIdeone
'CourseOf 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 Macrosmacros allowed punctuation... Thenpunctuation, then we could make it look operator-like. But alas, they don’t...
Edit: I do not think this question is a dupe to http://codereview.stackexchange.com/q/158078/78399 , since while the problem is same, the solutions are substantially different. This side is called "CodeReview", so it is about reviewing codes, so I think the criterion here is not whether two problems are same, but whether two codes are same. Well, here, they are not.
I freely admit that I posted those two questions mostly to ask You if the whole idea makes any sense and if it does, which of the two solutions is betterdon't.
Allowing switching between `operator[]` and `at()` based on `NDEBUG` Macro Version
'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...
Edit: I do not think this question is a dupe to http://codereview.stackexchange.com/q/158078/78399 , since while the problem is same, the solutions are substantially different. This side is called "CodeReview", so it is about reviewing codes, so I think the criterion here is not whether two problems are same, but whether two codes are same. Well, here, they are not.
I freely admit that I posted those two questions mostly to ask You if the whole idea makes any sense and if it does, which of the two solutions is better.
Allowing switching between operator[] and at() based on NDEBUG macro version
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.