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 7843015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:27:01+00:00 2026-06-02T16:27:01+00:00

Possible Duplicate: (static initialization/template instantiation) problems with factory pattern trying to force static object

  • 0

Possible Duplicate:
(static initialization/template instantiation) problems with factory pattern
trying to force static object initialization

EDIT: There is a duplicate of this but I’ll leave this up as I personally had trouble finding it. In addition here’s the answer that helped me:

https://stackoverflow.com/a/2852234/673730

Assume the following class:

template<class X>
struct A
{
   static bool x;
   static bool foo()
   {
      cout << "here";
      return true;
   }
};

template<class X>
bool A<X>::x = A<X>::foo();

I would have assumed that when I specialize A, the static field x would get initialized. However, the following:

A<int> a;
//no output

doesn’t result in a call to foo. If I try to access the member, the behavior is as expected:

A<int> a;
bool b = a.x;
//output: here

EDIT: How can I make sure A::x is initialized without accessing it?

  • 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-06-02T16:27:03+00:00Added an answer on June 2, 2026 at 4:27 pm

    This think is the reference(14.7.1.2) :

    Unless a member of a class template or a member template has been explicitly instantiated or explicitly specialized, the specialization of the member is implicitly instantiated when the specialization is referenced in a context that requires the member definition to exist; in particular, the initialization (and any associated side-effects) of a static data member does not occur unless the static data member is itself used in a way that requires the definition of the static data member to exist.

    template<class X, bool y>
    struct A
    {
        static cosnt bool x = y;
        static bool foo()
        {
           cout << "here";
           return true;
        }
     };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Get class of generic I'm trying to do this: public static<T extends
Possible Duplicate: Why aren't static const floats allowed? Is there any reason why this
Possible Duplicate: Do static members of a class occupy memory if no object of
Possible Duplicate: Returning in a static initializer Is there a way to exit a
Possible Duplicate: What is the static variable initialization order in C#? For fun i
Possible Duplicate: How to tell whether I’m static or an object? Let's say I
Possible Duplicate: .NET: Determine the type of this class in its static method Hello
Possible Duplicate: Efficient way to implement singleton pattern in Java I was reading this
Possible Duplicate: C++ singleton vs completely static object Hi, why should I prefer a
Possible Duplicate: Static extension methods So I know that Extension methods are for object

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.