Basically I'd like to do something like that:
int[3] array_func()
{
return {1,1,1};
}
int main(int argc,char * argv[])
{
int[3] point=array_func();
}
But that doesn't seem legal in C++. I know I can use vectors, but since I know the size of the array is a constant, it seems like a loss of performance is likely to occur.
I'd also like to avoid a new
if I can, because allocating stuff on the stack is easier and also likely to improve performance.
What's the solution here?
Using C++0x, the almost finalized new C++ standard (already implemented in latest gcc and msvc IIRC), you can do it exactly as you want! Simply use std::array instead of int[3].
std::array<int, 3> array_func()
{
return {1,1,1};
}
int main(int argc,char * argv[])
{
std::array<int, 3> point = array_func();
}