locked
Compound Literals Support? RRS feed

  • Question

  • I was under the impression that c99-style compound literals were to be supported in MSVC 14.  I am trying to intergrate the open source FFmpeg library with an MSVC 14 x64 project I am working on.  FFmpeg, which is written and compiled in C, is littered with code of the form: function_name((char[STRING_SIZE]){0}).  The function parameter is supposed to create a temporary char array and pass it into the function.  While this line compiles when the source is built as C, it fails when it is built as C++.  The build also fails when the include file statements are placed in extern "C" brackets.  Is this proper behavior, or lack of proper support as promised?  Is there a workaround for this issue that doesn't involve editing a well-established open source library?
    Wednesday, August 24, 2016 11:03 PM

Answers

  • Hi CaptainSteyr,

    thanks for posting here.

    >>Is this proper behavior, or lack of proper support as promised?  Is there a workaround for this issue that doesn't involve editing a well-established open source library?

    Compound Literals is a C99 construct, which GCC also supports in its C++ compiler as an extension. As far as I know, compound literals are not supported by MSVC in either its C or C++ mode. MSVC is not conformant to C99 and only loosely conformant to previous versions of the C standard. But the same effect can be obtained by using static const structs instead of anonymous compound literal constants, and local struct variables that are initialized with the correct values instead of anonymous compound literals that are nonconstant.

    Here are some documents, you could refer to.

    https://github.com/SonarOpenCommunity/sonar-cxx/wiki/Supported-compiler-specific-extensions

    https://en.m.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B

    https://blogs.gnome.org/rbultje/2012/09/27/microsoft-visual-studio-support-in-ffmpeg-and-libav/

    Best Regards,

    Sera Yu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    • Proposed as answer by Baron Bi Friday, September 2, 2016 8:04 AM
    • Marked as answer by Hart Wang Wednesday, September 7, 2016 2:01 AM
    Thursday, August 25, 2016 6:21 AM

All replies

  • Hi CaptainSteyr,

    thanks for posting here.

    >>Is this proper behavior, or lack of proper support as promised?  Is there a workaround for this issue that doesn't involve editing a well-established open source library?

    Compound Literals is a C99 construct, which GCC also supports in its C++ compiler as an extension. As far as I know, compound literals are not supported by MSVC in either its C or C++ mode. MSVC is not conformant to C99 and only loosely conformant to previous versions of the C standard. But the same effect can be obtained by using static const structs instead of anonymous compound literal constants, and local struct variables that are initialized with the correct values instead of anonymous compound literals that are nonconstant.

    Here are some documents, you could refer to.

    https://github.com/SonarOpenCommunity/sonar-cxx/wiki/Supported-compiler-specific-extensions

    https://en.m.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B

    https://blogs.gnome.org/rbultje/2012/09/27/microsoft-visual-studio-support-in-ffmpeg-and-libav/

    Best Regards,

    Sera Yu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    • Proposed as answer by Baron Bi Friday, September 2, 2016 8:04 AM
    • Marked as answer by Hart Wang Wednesday, September 7, 2016 2:01 AM
    Thursday, August 25, 2016 6:21 AM
  • The c99 syntax seems only work in C mode, not C++.  

    //Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86
    
    #include <stdio.h>
    
    typedef struct s {
      int mi;
      float mf;
        char const *mp;
    } s;
    
    int main()
    {
        printf("Hello, world!\n");
        
    int* ptr;
    ptr = (int[]){ 10, 20, 30, 40 };
    printf("%d %d\n", ptr[1], ptr[2] );
    
    struct s pi;
    pi = (struct s){ 3, 3.1415, "Pi" };
    printf("%s\n", pi.mp );
     
        
    }

    -- pa


    • Edited by Pavel A Sunday, August 28, 2016 4:11 PM
    • Proposed as answer by Pavel A Thursday, September 1, 2016 11:47 PM
    Sunday, August 28, 2016 4:09 PM