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

The Archive Base Latest Questions

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

Suppose I have such a template class: template <class T> class Queue { public:

  • 0

Suppose I have such a template class:

template <class T>
class Queue
{
public:
    static int Size;
};

template <class T> int Queue<T>::Size = 0;

And I export a function in D.dll use Queue as an parameter:

void ChangeQueueSize(Queue<int>& q)
{
    q.Size = 100;
}

And then I use this exported function in an A.exe:

Queue<int> q;

q.Size = 10; 

ChangeQueueSize(q);

int updatedSize = q.Size;

Since the Queue class is generated from the class template in 2 projects, there are actually 2 copies of the code, as well as the static data member.

So calling ChangeQueueSize won’t really change queue size here, it just update another class’s static member, which happens to be have the same class name.

What can we do to solve this problem?
Is weak symbol in gcc capable of address this?
Thanks so much.

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

    You cannot put templates in a library in the way you might think. You can only put actual, instantiated class definitions in a library.

    Templates are essentially a code generation tool, and you can only put generated code into the library.

    You might want to use explicit template instantiation to make the compiler generate the code, and take the static member definition out of the header:

    // Header, shipped to clients
    template <class T>
    class Queue
    {
    public:
        static int Size;
    };
    
    // Library source code:
    template <typename T> int Queue<T>::size = 0;
    
    template class Queue<int>;
    

    Now compile the source file into the library; this will contain the instance of the static member variable Queue<int>::size.

    Note that your consumers will only be able to use instances of your class with T = int since they don’t have access to the static member otherwise (i.e. they’d have to provide their own).

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

Sidebar

Related Questions

Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {
Let's suppose I have such helper methods used by some object. private int MatchRegex(string
Suppose I have a template which takes 3 template parameter. I want the third
Suppose I have written such a class (number of functions doesn't really matter, but
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose I have BaseClass with public methods A and B, and I create DerivedClass
The problem: I have a class which contains a template method execute which calls
Suppose I have a structure in C or C++, such as: struct ConfigurableElement {
I have the following problem: Suppose I have some basic counter class Counter .
Suppose I have a state monad such as: data Registers = Reg {...} data

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.