Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass array to function without variable instantiation, in C++

Tags:

c++

Can I do this in C++ (if yes, what is the syntax?):

void func(string* strs) {
    // do something
}
func({"abc", "cde"});

I want to pass an array to a function, without instantiating it as a variable.

like image 920
yegor256 Avatar asked May 04 '10 16:05

yegor256


1 Answers

It can't be done in the current C++, as defined by C++03.

The feature you are looking for is called "compound literals". It is present in C language, as defined by C99 (with C-specific capabilities, of course), but not in C++.

A similar feature is planned for C++ as well, but it is not there yet.

like image 69
AnT Avatar answered Sep 28 '22 05:09

AnT