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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:11:33+00:00 2026-05-12T17:11:33+00:00

I can’t understand, why if we define static variable of usual (non-template) class in

  • 0

I can’t understand, why if we define static variable of usual (non-template) class in header, we have linker error, but in case of templates all works fine and moreover we will have single instance of static variable among all translation units:

It’s template header (template.h):

// template.h
template<typename T>
class Templ {
public:
  static int templStatic;
};

template<typename T> Templ<T>::templStatic = 0;

It’s first unit using template (unit1.cpp)

// unit1.cpp
#include "template.h"

int method1() {
  return Templ<void>::templStatic++;
}

Second unit here (unit2.cpp):

// unit2.cpp
#include "template.h"
int method2() {
  return Templ<void>::templStatic++;
}

And, finally, main.cpp:

// main.cpp
#include <iostream>
int method1();
int method2();

int main(int argc, char** argv) {
  std::cout << method1() << std::endl;
  std::cout << method2() << std::endl;
}

After compilling, linking and executing this code, we will have following output:

0
1

So, why in case of templates all works fine (and as expected) ? How compiler or linker handle this (we can compile each .cpp file in separated calling of compiler, and then link them with caling to linker, so compiler and linker don’t “see” all .cpp files at same time) ?

PS: My compiler: msvcpp 9 (but checked on mingw too)

  • 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-12T17:11:33+00:00Added an answer on May 12, 2026 at 5:11 pm

    It’s because the definition of the static data member is itself a template. Allowing this is necessary for the same reason you are allowed to have a function template that’s not inline multiple times in a program. You need the template to generate the resulting entity (say, a function, or a static data member). If you wouldn’t be allowed to put the definition of a static data member, how would you instantiate the following

    template<typename T>
    struct F {
      static int const value;
    };
    
    template<typename T>
    int const F<T>::value = sizeof(T);
    

    It’s not known what T is – the Standard says the definition outside the class template is a template definition, in which the parameters are inherited from its class template owner.


    I’ve made some experiment with GCC. In the following, we have one implicit instantiation of F<float>::value, and one explicit specialization of F<char>::value which has to be defined in a .cpp file to not cause duplicated symbol errors when included multiple times.

    // Translation Unit 1
    template<typename T>
    struct F {
      static int value; 
    };
    
    template<typename T>
    int F<T>::value = sizeof(T);
    
    // this would belong into a .cpp file
    template<> int F<char>::value = 2;
    
    // this implicitly instantiates F<float>::value
    int test = F<float>::value;
    
    int main() { }
    

    The second translation unit contains just another implicit instantiation of the same static data member

    template<typename T>
    struct F {
      static int value; 
    };
    
    template<typename T>
    int F<T>::value = sizeof(T);
    
    int test1 = F<float>::value;
    

    Here is what we get with GCC – it makes each implicit instantiation into a weak symbols and sticks it into its own section here. Weak symbols will not cause errors when there exist multiple of them at link time. Instead, the linker will choose one instance, and discards the other ones assuming all of them are the same

    objdump -Ct main1.o # =>
    # cut down to the important ones
    00000000 l    df *ABS*  00000000 main1.cpp
    0000000a l     F .text  0000001e __static_initialization_and_destruction_0(int, int)
    00000000 l    d  .data._ZN1FIfE5valueE  00000000 .data._ZN1FIfE5valueE
    00000028 l     F .text  0000001c global constructors keyed to _ZN1FIcE5valueE
    00000000 g     O .data  00000004 F<char>::value
    00000000 g     O .bss   00000004 test
    00000000 g     F .text  0000000a main
    00000000  w    O .data._ZN1FIfE5valueE  00000004 F<float>::value
    

    So as we can see F<float>::value is a weak symbol which means the linker can see multiple of these at link time. test, main and F<char>::value are global (non-weak) symbols. Linking main1.o and main2.o together, we see in the map output (-Wl,-M) the following

    # (mangled name)
    .data._ZN1FIfE5valueE
        0x080497ac        0x4 main1.o                                             
        0x080497ac                F<float>::value
    

    This indicates that actually it drops all except one instance.

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

Sidebar

Related Questions

Can find why i get this error can someone help? package Android.data; public class
Can I order my users in the database, so I don't have to say
I have a jquery bug and I've been looking for hours now, I can't
Can you guys help me understand a concept real quick, I'm having trouble understanding
can someone explain why the compiler accepts only this code template<typename L, size_t offset,
Can I register a .Net COM class with the SingleUse -flag? The reason I
Can't figure out how to do this in a pretty way : I have
Can you have submenus with the top level set to checkable in WPF? I
Can someone please tell me why this wont work? I am getting error preg_match_all():
Can anyone let me know how can we change the value of kendo combobox

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.