c++gcccompiler-errorsbraces

Ignoring GCC "error: braces around scalar initializer for type" errors. Make them warnings


I have a nagging problem with GCC compiler errors "error: braces around scalar initializer for type". I have seen others complaining about this, although they describe it as a warning (gcc warning: braces around scalar initializer)

I am compiling code which is not mine to edit, and I get a lot of these errors throughout the code.

Basic Pattern is:

struct t_
{
    float f;
    int i;
};

float f = { 0.3 };      //Compiler is all happy with this.
int i = {0};            //Compiler is all happy with this too.
t_ t1 = { 0.3, 0 };     //Compiler is all happy with this too.
t_ t2 = { {0.3}, 0 };   //Compiler ERROR: braces around scalar initializer for type 'float' 

I know I can remove the braces {} around the float scaler to remove this error, but I do not want to modify the code in any way. Is there a flag I can give to GCC (currently using MinGW gcc 4.8.1). i.e. "std=c++03", or something to get these errors at least displayed as warnings.


Solution

  • I'm not 100% sure, but I believe there is no such option. The construct you have is not meaning the same thing in the two cases - first one is an initialization of one structure, the second is a a strcuture containing a structure or array. Which of course float isn't.

    You may be able to work around it with

    struct t_
    {
        struct 
        {
           float f;
        };
        int i;
    };
    

    At least clang is happy with that. As is g++. That may be easier than changing a lot of initialization statements with extra braces in them. But it is admittedly still a change to the source code. Unfortunately, I'm pretty certain that this is necessary.

    Complete example that I was testing with:

    struct t_
    {
        struct 
        {
            float f;
        };
        int i;
    };
    
    
    
    t_ t2 = { {0.3}, 0 };
    
    
    int main()
    {
        t2.f = 7;
    }
    

    Edit: If it's not at all possible to edit the source, you'll need to parse the source code, identify the incorrect braces and output "correct" code. The more I think about this, the less I believe that it's at all possible to fix without some sort of edit to the source. Or that it has ever compiled...