I\'ve been using C++ for a few years, and today I saw some code, but how can this be perfectly legal?
int main(int argc, char **argv)
{
size_t size;
I recently came across a scenario where a stack-allocated array is desired. (It's a wrapper around v8, needed an array of args on every method call).
An std::vector would do heap memory allocation, whose performance is not acceptable.
Here is my solution, use template to allocation array of cases:
template
static void call(...) {
v8::Local v8Args[Argc];
// use v8Args
...
}
template
static void callV8Function(size_t argc, It argvBegin, It argvEnd,) {
// C++ don't have dynamic stack allocation (like C99 does)
// try to avoid heap-allocation...
if (argc <= 4) {
return callV8FunctionOnStack<4>(...);
} else if (argc <= 8) {
return callV8FunctionOnStack<8>(...);
} else if (argc <= 16) {
return callV8FunctionOnStack<16>(...);
} else if (argc <= 32) {
return callV8FunctionOnStack< 32>(...);
} else {
std::vector> v8Args(argc);
// fallback to vector
}
}
(And of course, I can just do with a 32-sized array, but which is not so elegant.)