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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:44:15+00:00 2026-05-24T16:44:15+00:00

If a template class definition contains a static member variable that depends on the

  • 0

If a template class definition contains a static member variable that depends on the template type, I’m unsure of what the reliable behavior should be?

In my case it is desirable to place the definition of that static member in the same .h file as the class definition, since

  1. I want the class to be general for many template data types that I don’t currently
    know.
  2. I want only one instance of the static member to be shared
    throughout my program for each given template type. ( one for all MyClass<int> and one for all MyClass<double>, etc.

I can be most brief by saying that the code listed at this link behaves exactly as I want when compiled with gcc 4.3. Is this behavior according to the C++ Standard so that I can rely on it when using other compilers?

That link is not my code, but a counter example posted by CodeMedic to the discussion here. I’ve found several other debates like this one but nothing I consider conclusive.

I think the linker is consolidating the multiple definitions found ( in the example a.o and b.o ).
Is this the required/reliable linker behavior?

  • 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-24T16:44:17+00:00Added an answer on May 24, 2026 at 4:44 pm

    From N3290, 14.6:

    A […] static data member of a class template shall be defined in
    every translation unit in which it is implicitly instantiated […], unless the corresponding specialization is explicitly instantiated […] .

    Typically, you put the static member definition in the header file, along with the template class definition:

    template <typename T>
    class Foo
    {
      static int n;                       // declaration
    };
    
    template <typename T> int Foo<T>::n;  // definition
    

    To expand on the concession: If you plan on using explicit instantiations in your code, like:

    template <> int Foo<int>::n = 12;
    

    then you must not put the templated definition in the header if Foo<int> is also used in other TUs other than the one containing the explicit instantiation, since you’d then get multiple definitions.

    However, if you do need to set an initial value for all possible parameters without using explicit instantiation, you have to put that in the header, e.g. with TMP:

    // in the header
    template <typename T> int Foo<T>::n = GetInitialValue<T>::value;  // definition + initialization
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ template class that gets instantiated with 3 different type parameters.
$14.6.2/3 - In the definition of a class template or a member of a
I have a class with several template member functions that I would like to
I have a template class definition that needs to be in the cpp file.
I have a template class that I serialize (call it C), for which I
I have a template class that is only valid for couple of template parameters:
I have a template class where I want to use objects of that class
I have two PHP files: template.php template.html.php The first is the class definition for
I have a base.html template that contains a list of links. Example: <div id=sidebar1>
Given a C++ template class (or function) definition: template<typename T> struct Foo { T

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.