Is this C++11 or whatever ?
I have a project here which I still need to build with MSVC 8.0 (VS2005). Today, someone upstream added the following code:-
Code:
// In a header file:-
typedef enum _cairo_svg_unit {
CAIRO_SVG_UNIT_USER = 0,
CAIRO_SVG_UNIT_EM,
CAIRO_SVG_UNIT_EX,
CAIRO_SVG_UNIT_PX,
CAIRO_SVG_UNIT_IN,
CAIRO_SVG_UNIT_CM,
CAIRO_SVG_UNIT_MM,
CAIRO_SVG_UNIT_PT,
CAIRO_SVG_UNIT_PC,
CAIRO_SVG_UNIT_PERCENT
} cairo_svg_unit_t;
// In a source file:-
static const char * _cairo_svg_unit_strings[] =
{
[CAIRO_SVG_UNIT_USER] = "", // <--- error occurs here !!
[CAIRO_SVG_UNIT_EM] = "em",
[CAIRO_SVG_UNIT_EX] = "ex",
[CAIRO_SVG_UNIT_PX] = "px",
[CAIRO_SVG_UNIT_IN] = "in",
[CAIRO_SVG_UNIT_CM] = "cm",
[CAIRO_SVG_UNIT_MM] = "mm",
[CAIRO_SVG_UNIT_PT] = "pt",
[CAIRO_SVG_UNIT_PC] = "pc",
[CAIRO_SVG_UNIT_PERCENT] = "%"
};
MSVC complains giving me error C2059: syntax error : '['
I'm guessing this is some unsupported syntax which got added later than 2005?
Re: Is this C++11 or whatever ?
This doesn't compile as c++ in VS2017 either - same issue! It looks like the code is trying to specify the element of the array to be initialised. AFAIK this isn't standard c++ syntax.
To get this to compile, something like
Code:
// In a source file:-
static const char * _cairo_svg_unit_strings[] =
{
"", // <--- error occurs here !!
"em",
"ex",
"px",
"in",
"cm",
"mm",
"pt",
"pc",
"%"
};
Re: Is this C++11 or whatever ?
This is C99 designated initialization. See Initialization from brace-enclosed lists section here.
Code:
#include <stdio.h>
const char * s0[] = {
"s0_0",
"s0_1",
NULL,
};
#ifdef C99_INIT
const char * s1[] = {
[0] = "s1_0",
[1] = "s1_1",
[2] = NULL,
};
#endif
int main()
{
for (const char **s = s0; *s != NULL; s++)
printf("%s\n", *s);
#ifdef C99_INIT
for (const char **s = s1; *s != NULL; s++)
printf("%s\n", *s);
#endif
return 0;
}
Linux:
Code:
$ g++ 142.cpp
$ ./a.out
s0_0
s0_1
$ g++ 142.cpp -DC99_INIT
$ ./a.out
s0_0
s0_1
s1_0
s1_1
Windows, VS2012:
Code:
J:\Temp\142>cl 142.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.50727.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
142.cpp
Microsoft (R) Incremental Linker Version 11.00.50727.1
Copyright (C) Microsoft Corporation. All rights reserved.
/out:142.exe
142.obj
J:\Temp\142>142
s0_0
s0_1
J:\Temp\142>cl 142.cpp /DC99_INIT
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.50727.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
142.cpp
142.cpp(11) : error C2143: syntax error : missing ']' before 'constant'
142.cpp(11) : error C2059: syntax error : ']'
142.cpp(11) : error C2059: syntax error : 'constant'
142.cpp(14) : error C2143: syntax error : missing ';' before '}'
142.cpp(14) : error C2059: syntax error : '}'
Re: Is this C++11 or whatever ?