Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 91461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:02:01+00:00 2026-05-10T23:02:01+00:00

I noticed C++ will not compile the following: class No_Good { static double const

  • 0

I noticed C++ will not compile the following:

class No_Good {   static double const d = 1.0; }; 

However it will happily allow a variation where the double is changed to an int, unsigned, or any integral type:

class Happy_Times {   static unsigned const u = 1; }; 

My solution was to alter it to read:

class Now_Good {   static double d() { return 1.0; } }; 

and figure that the compiler will be smart enough to inline where necessary… but it left me curious.

Why would the C++ designer(s) allow me to static const an int or unsigned, but not a double?

Edit: I am using visual studio 7.1 (.net 2003) on Windows XP.

Edit2:

Question has been answered, but for completion, the error I was seeing:

error C2864: 'd' : only const static integral data members can be initialized inside a class or struct 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:02:01+00:00Added an answer on May 10, 2026 at 11:02 pm

    The problem is that with an integer, the compiler usually doesn’t have to ever create a memory address for the constant. It doesn’t exist at runtime, and every use of it gets inlined into the surrounding code. It can still decide to give it a memory location – if its address is ever taken (or if it’s passed by const reference to a function), that it must. In order to give it an address, it needs to be defined in some translation unit. And in that case, you need to separate the declaration from the definition, since otherwise it would get defined in multiple translation units.

    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:

    // File a.h class X {  public:   static const double d = 1.0; };  void foo(void);  // File a.cc #include <stdio.h>  #include 'a.h'  int main(void) {   foo();   printf('%g\n', X::d);    return 0; }  // File b.cc #include <stdio.h>  #include 'a.h'  void foo(void) {   printf('foo: %g\n', X::d); } 

    Command line:

    g++ a.cc b.cc -O0 -o a   # Linker error: ld: undefined symbols: X::d g++ a.cc b.cc -O1 -o a   # Succeeds 

    For maximal portability, you should declare your constants in header files and define them once in some source file. With no optimization, this will not hurt performance, since you’re not optimizing anyways, but with optimizations enabled, this can hurt performance, since the compiler can no longer inline those constants into other source files, unless you enable ‘whole program optimization’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Related Questions

Loading...

Sidebar

Ask A Question

Stats

  • Questions 54k
  • Answers 54k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You have a misunderstanding of how asp.net works. Your server… May 11, 2026 at 7:28 am
  • added an answer The following is probably not the neatest way, but it… May 11, 2026 at 7:28 am
  • added an answer When do they fire? window.onload By default, it is fired… May 11, 2026 at 7:28 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.