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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:28:23+00:00 2026-05-28T13:28:23+00:00

How would one initialize static data members of a template class differently for particular

  • 0

How would one initialize static data members of a template class differently for particular parameters?

I understand that templates are different than other kinds of classes and only what is used in the project ever gets instantiated. Can I list a number of different initializations for different parameters and have the compiler use whichever is appropriate?

For example, does the following work, and if not what is the correct way to do this? :

template<class T>
class someClass
{
   static T someData;
   // other data, functions, etc...
};

template<class T>
T someClass::someData = T.getValue();

template<>
int someClass<int>::someData = 5;

template<>
double someClass<double>::someData = 5.0;

// etc...
  • 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-28T13:28:24+00:00Added an answer on May 28, 2026 at 1:28 pm

    Should work. You may need to put these into the .c file instead of the header.

    int someClass<int>::someData = 5;
    double someClass<double>::someData = 5.0;
    

    Here is also a working partial template specialization with initialization of static data members:

    // .h
    template <class T, bool O>
    struct Foo {
        T *d_ptr;
        static short id;
        Foo(T *ptr) : d_ptr(ptr) { }
    };
    template <class T>
    struct Foo<T, true> {
        T *d_ptr;
        static short id;
        Foo(T *ptr) : d_ptr(ptr) { }
    };
    template<class T, bool O>
    short Foo<T, O>::id = 0;
    template<class T>
    short Foo<T, true>::id = 1;
    
    //.cpp
    int main(int argc, char *argv[])
    {
        Foo<int, true> ft(0);
        Foo<int, false> ff(0);
        cout << ft.id << " " << ff.id << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that only has static members. I would like to register
I understand that one benefit of having static member functions is not having to
To value initialize an object of type T , one would do something along
How would one create a Singleton class using PHP5 classes?
How would one structure a table for an entity that can have a one
How would one go about profiling a few queries that are being run from
Which static class initializes first if we have one more static classes in our
I know I'm missing one important rule about static methods but would would be
I am trying to initialize an expensive object, through .NET's Lazy class, that can
In C++, I know that the compiler can choose to initialize static objects in

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.