mercredi 8 mars 2017

C++ and C++11 class static member, double should use "constexpr" while int can be "const", why?

A structure C defined several static const members like this:

Code is like below:

#include<stdio.h>
struct C{
    static int i;
    static const int j=1;
    static constexpr double d=1;
    static const double d1=1.0;
};
int main(){
    return 0;
}

Compilation will lead to error:

$g++ testStatic.cpp -std=c++11
testStatic.cpp:6:25: error: in-class initializer for static data member of
      type 'const double' requires 'constexpr' specifier
      [-Wstatic-float-init]
    static const double d1=1.0;
                        ^  ~~~
testStatic.cpp:6:5: note: add 'constexpr'
    static const double d1=1.0;
    ^
    constexpr
1 error generated.

Why so weird Why static int can be const,double should be constexpr,what's the rational

Aucun commentaire:

Enregistrer un commentaire