0

I read about 5 SOF questions and they all say that we can only initialize static const integral types.

double is NOT integral type. Then why this code works? It should give error, shouldn't it?

class Simple
{
    static const double x = 5.5;
};

int main(){}

Note: I am NOT using C++11.

Vipulcoolrule
  • 31
  • 1
  • 7
  • It [*doesn't* work](http://coliru.stacked-crooked.com/a/94e308b046b797f7). Your compiler is not being nice to you. Maybe it's time to look for [one](http://clang.llvm.org/) that respects you more. – Kerrek SB Nov 07 '14 at 05:23
  • possible duplicate of [Why can't I have a non-integral static const member in a class?](http://stackoverflow.com/questions/370283/why-cant-i-have-a-non-integral-static-const-member-in-a-class) – Jason C Nov 07 '14 at 05:24
  • ^ Note accepted answer mentions case where GCC will accept this code depending on the optimization level, and gives explanation of this issue. I can imagine the same iffiness holding true for other compilers. – Jason C Nov 07 '14 at 05:25
  • Hi Kerrek SB, I am using Code Blocks with MinGNU compiler. Compiler came pre-installed with CB version 10.05 @Jason It's not duplicate. Read full thread. What do you mean by that optimization level. Can you explain more please? – Vipulcoolrule Nov 07 '14 at 05:25
  • @Vipulcoolrule: Configure it better so it becomes more honest. Try `-Wall -Wextra -pedantic`. – Kerrek SB Nov 07 '14 at 05:26
  • 2
    @Vipulcoolrule It is a duplicate. Read the full thread; your question is answered there. – Jason C Nov 07 '14 at 05:27
  • Jason It's title was different so maybe I didn't check it out. I'll check it out now. @Kerrek It says this in Linker settings: -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32 – Vipulcoolrule Nov 07 '14 at 05:29
  • So mine is highly optimized? Is it a cool thing? :) And what is -0tetha and -01? And it's not duplicate of thread question. It's just that somehow it was answered in it. :) – Vipulcoolrule Nov 07 '14 at 05:41
  • 1
    @Vipulcoolrule I'm not sure how you got from "accepts ill-formed code" to "highly optimized." Being tolerant of broken code has nothing to do with optimization. – cdhowie Nov 07 '14 at 06:19
  • @cdhowie By reading this: Using g++ with no optimization (-O0), it automatically inlines constant integer variables but not constant double values. At higher optimization levels (e.g. -O1), it inlines constant doubles. Thus, the following code compiles at -O1 but NOT at -O0 – Vipulcoolrule Nov 08 '14 at 05:00
  • @Vipulcoolrule That sounds like a bug. The level of optimization should not affect whether a program is accepted as valid code. – cdhowie Nov 08 '14 at 05:05

0 Answers0