initiate array inside function call [closed]
My Question is related to the release of the c++11 standard and this old question, as I wondered if it is now possible to create an array/vector inside a function call, instead of building the array/vector before and then just giving it as an parameter to the method/function.
(Presuming you're talking about C++11.)
void f(int x[]) {} // remember, same as void f(int* x) {}
int main() { f({0,1,2}); }
// error: cannot convert '<brace-enclosed initializer list>'
// to 'int*' for argument '1' to 'void f(int*)'
But:
void f(const int (&x)[3]) {}
int main() { f({0,1,2}); }
// <no output>
And:
void f(std::array<int, 3> x) {}
int main() { f({0,1,2}); }
// <no output>
And, incidentally:
void f(std::vector<int> x) {}
int main() { f({0,1,2}); }
// <no output>
So essentially yes, but with caveats.
精彩评论