Is this C++11 or whatever ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Is this C++11 or whatever ?

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,453

    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?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,872

    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",
    		 "%"
    	};
    Last edited by 2kaud; December 5th, 2017 at 05:42 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,578

    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 : '}'
    Last edited by Igor Vartanov; December 5th, 2017 at 05:58 AM.
    Best regards,
    Igor

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,453

    Re: Is this C++11 or whatever ?

    Thanks guys...
    "A problem well stated is a problem half solved. - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)