mercredi 22 juillet 2015

Non-static member initialization of char array with brace gives an error in gcc while not in clang

Consider following code:

#include <iostream>

class A
{
    char name[40] = { "Blank" }; // note the braces here
public:
    const char *getName() { return name; }
};

int main()
{
    A a;

    std::cout << a.getName() << std::endl;
}

It gives an error in gcc (latest version 5.2.0):

prog.cpp:5:28: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
  char name[40] = { "Blank" };
                            ^

But it's not the case for clang, that compiles it flawlessly with -std=c++11 -pedantic -Wall.

Is it really incorrect to put braces for non-static initializer here?

AFAIR it doesn't matter if braces are present or not. For instance, the definition of array, such as:

char text[] = "some text";

is equivalent to:

char text[] = { "some text" };

Aucun commentaire:

Enregistrer un commentaire