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

  • Home
  • SEARCH
  • 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 6689651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:32:40+00:00 2026-05-26T05:32:40+00:00

Why does the Visual C++ compiler refuse to compile this code ? I obviously

  • 0

Why does the Visual C++ compiler refuse to compile this code?

I obviously know that the error is:

Error C2864: Singleton<T>::p:
Only static const integral data members can be initialized within a class

but why? (i.e. is there a technical reason why it is not allowed?)
Is this compiler-specific behavior or is it mandated by the standard?
It seems to be fine at the global scope, so why not at the class scope?

It also seems like not all compilers mind this.

Also, what is the proper way of fixing this?

template<typename T>
struct Singleton
{
    static T *p = 0;  // Error C2864

    static T *getInstance() { /*...*/ return p; }
};
  • 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. Editorial Team
    Editorial Team
    2026-05-26T05:32:41+00:00Added an answer on May 26, 2026 at 5:32 am

    That’s standard behavior. Only static const integral members can be initialized without a proper definition. All other types need to be defined somewhere, and the initialization is written at the point of definition:

    template<typename T>
    struct Singleton {
        static T *p;
    
        static T *getInstance() { /*...*/ return p; }
    };
    
    template<typename T>
    T *Singleton<T>::p = 0;
    

    Objects must be defined somewhere. If you define them within your class then its defined at a header file, and you get a different object for each compilation unit that includes it. This is relaxed for const integral types and if you don’t define them then the compiler just replaces it with its literal value. Taking the address of such static const integral would still result in a linker error if no definition is provided.

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

Sidebar

Related Questions

The Visual Studio compiler does not seem to warn on signed/unsigned assignments, only on
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
I'm interested in how the Microsoft Visual C++ compiler treat/optimize static variables. My code:
I was wondering if, and to what degree, does Microsoft's Visual C++ compiler conform
The following method does not compile. Visual Studio warns An out parameter may not
Does Visual Studio .NET 2008 come with UISpy? I ask this for for the
Does Visual Studio / Resharper support this? By refactor copy I mean to be
I'm looking for the compiler options in Visual Studio 2010 (Visual C#). Does anyone
I know that Visual Studio under debugging options will fill memory with a known
I have been in this situation quite a few times where visual studio does

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.