开发者

initiate array inside function call [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vagu开发者_StackOverflow中文版e, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜