3

I have declared and initialized a constant char array within a class:

class grid {
    const char test[11] = {'s', 'e', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

My code works, but I keep getting compiler warnings:

non static data member initializers only available with C++11

and

extended initializer lists only available with C++11

I know that this isn't an issue because I'm compiling to C++11 standard, but I'm curious as to what is pre C++11 about my code.

I am hoping someone can give me some insight and suggest what I can do to make this code C++98 "friendly".

Also as requested, my compiler command:

> g++ -o test main.cpp
Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
ggle
  • 426
  • 3
  • 20
  • 2
    Did you add `-std=c++11` to the compiler command line? – Praetorian Jun 24 '14 at 19:04
  • 1
    it is [Enabled By Default] in the warning – ggle Jun 24 '14 at 19:05
  • Can you show your compiler command line in your question? – simonc Jun 24 '14 at 19:06
  • 1
    To be C++98 "friendly" as you call it, don't initialise your class data members in the class declaration; do that in the class constructor instead. – djikay Jun 24 '14 at 19:06
  • 3
    [Enabled by default](https://stackoverflow.com/questions/13122636/c-what-does-the-warning-enabled-by-default-mean-during-compile) means the warning is enabled by default, it doesn't have anything to do with `-std=c++11`. This warning shouldn't be appearing if you have C++11 mode enabled. C++03 only allows in class initialization of constant integral static data members. You're using a C++11 feature called non-static data member initializer. – Praetorian Jun 24 '14 at 19:08
  • @simonc added as requested – ggle Jun 24 '14 at 19:16
  • 3
    You can't initialize arrays in the constructor in C++98, though. Unless you call assigning to elements initializing. – chris Jun 24 '14 at 19:18
  • @Rhys you do not have C++11 enabled, to do so compile with command `g++ -std=c++11 -o test main.cpp` and this warning will disappear – Slava Jun 24 '14 at 19:58

2 Answers2

2

You need do compile with -std=c++11 (for gcc and clang). If you do not do this, your program is checked vs. the old C++98 standard to ensure compatibility with old compilers.

So it allows you to use C++11 features without the flag, but warns you so that you don't do it on accident.

Further explanation:

Your code compiles fine because it is legal code and the compiler can compile it. The compiler omits a warning to make you aware of the fact that you used a C++11 feature because many people (like my University, sadly) still use outdated compiler like gcc4.6 that do not have full C++11 support yet. That means that these people might not be able to compile your code, which you might care about (if e.g. your professor needs to compile your assignment).

With the -std=c++11 flag you tell the compiler "This is a C++11 program, meant to be compiled with C++11 compliant compilers". Thus, the warning becomes redundant.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
2

To make it C++98 compatible, you need to initialize non-static class constants outside of the class declaration.

dgross
  • 369
  • 1
  • 12