Re: anonymous array of strings // ("taking address of temporary"
- how long is temporary valid?)
* anon.asdf@gmail.com:
On 11 Feb., 16:06, anon.asdf wrote:
Hello!
1) ===============================
When trying to define an array of std::string ...
func( (std::string []) { std::string("ab"), std::string("cd"),
std::string("ef") } , 3 );
void func(std::string arr[], int n)
{
while ((--n) >= 0) {
std::cout << arr[n];
}
}
Interestingly, the following works fine:
//===================================
#include <iostream>
void func3(char* arr[], int n);
int main(void)
{
func3((char* []){"ab", "cd", "ef"}, 3);
return 0;
}
void func3(char* arr[], int n)
{
while ((--n) >= 0) {
std::cout << arr[n];
}
std::cout << std::endl;
}
//===================================
<quote>
V:\> gnuc vc_project.cpp
vc_project.cpp: In function `int main()':
vc_project.cpp:7: error: ISO C++ forbids compound-literals
V:\> msvc vc_project.cpp
vc_project.cpp
vc_project.cpp(7) : error C2143: syntax error : missing ')' before '{'
vc_project.cpp(7) : error C2059: syntax error : ')'
vc_project.cpp(7) : error C2143: syntax error : missing ';' before '{'
vc_project.cpp(7) : error C2143: syntax error : missing ';' before '}'
vc_project.cpp(7) : error C2143: syntax error : missing ';' before ','
vc_project.cpp(7) : error C2059: syntax error : ')'
V:\>_
</quote>
Can it be similarly adapted for std::string, without naming an array
variable?
No.
It seems the compiler+options you're using allows the above as an extension.
It might be that it's syntax that has been introduced in C99, but it's
not C++.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?