c++ string array initialization -
i know can in c++:
string s[] = {"hi", "there"};
but there anyway delcare array way without delcaring string s[]
?
e.g.
void foo(string[] strarray){ // code } string s[] = {"hi", "there"}; // works foo(s); // works foo(new string[]{"hi", "there"}); // doesn't work
in c++11 can. note beforehand: don't new
array, there's no need that.
first, string[] strarray
syntax error, should either string* strarray
or string strarray[]
. , assume it's sake of example don't pass size parameter.
#include <string> void foo(std::string* strarray, unsigned size){ // stuff... } template<class t> using alias = t; int main(){ foo(alias<std::string[]>{"hi", "there"}, 2); }
note better if didn't need pass array size parameter, , thankfully there way: templates!
template<unsigned n> void foo(int const (&arr)[n]){ // ... }
note match stack arrays, int x[5] = ...
. or temporary ones, created use of alias
above.
int main(){ foo(alias<int[]>{1, 2, 3}); }
Comments
Post a Comment