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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:43:59+00:00 2026-05-24T04:43:59+00:00

Consider this synthetic example. I have two native C++ projects in my Visual Studio

  • 0

Consider this synthetic example. I have two native C++ projects in my Visual Studio 2010 solution. One is console exe and another is lib.

There are two files in lib:

// TImage.h

template<class T> class TImage
{
public:
  TImage()
  {
#ifndef _LIB
    std::cout << "Created (main), ";
#else
    std::cout << "Created (lib), ";
#endif
    std::cout << sizeof(TImage<T>) << std::endl;
  }

#ifdef _LIB
  T c[10];
#endif
};

void CreateImageChar();
void CreateImageInt();

and

// TImage.cpp

void CreateImageChar()
{
  TImage<char> image;
  std::cout << sizeof(TImage<char>) << std::endl;
}
void CreateImageInt()
{
  TImage<int> image;
  std::cout << sizeof(TImage<int>) << std::endl;
}

And one file in exe:

// main.cpp

int _tmain(int argc, _TCHAR* argv[])
{
  TImage<char> image;
  std::cout << sizeof(TImage<char>) << std::endl;

  CreateImageChar();
  CreateImageInt();

  return 0;
}

I know, I shouldn’t actually do like this, but this is just for understanding what is happening. And that’s, what happens:

// cout:
Created (main), 1
1
Created (main), 1
10
Created (lib), 40
40

So how exactly this happened, that linker overrides lib’s version of TImage<char> with exe’s version of TImage<char>? But since there is no exe’s version of TImage<int>, it preserves lib’s version of TImage<int>?.. Is this behavior standardized, and if so, where can I found the description?

Update: Explanations of the effect given below are correct, thanks. But the question was “how exactly this happened”?.. I expected to get some linker error like “multiply defined symbols”. So the most suitable answer is from Antonio Pérez’s reply.

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

    Template code creates duplicated object code.

    The compiler copies the template code for the type you provide when you instance the template. So when TImage.cpp is compiled you get object code for two versions of your template, one for char and one for int in TImage.o. Then main.cpp is compiled and you get a new version of your template for char in main.o. Then the linker happens to use the one in main.o always.

    This explains why your output yields the ‘Created’ lines. But it was a little bit puzzling to see the mismatch in lines 3, 4 regarding object’s size:

    Created (main), 1
    10
    

    This is due to the compiler resolving the sizeof operator during compile-time.

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

Sidebar

Related Questions

Consider this example (typical in OOP books): I have an Animal class, where each
Consider this scenario that two WCF clients connect to one WCF service(server), this service
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider this example table (assuming SQL Server 2005): create table product_bill_of_materials ( parent_product_id int
Consider this simple example code: <form name=text id=frm1 method=post> <input type=checkbox name=name[] value=1000> Chk1<br>
Consider this scenario. We have an internal Rails 2 app that connects to a
consider this simple scenario: $this->method($arg1, $arg2); Solution: call_user_func_array(array($this,'method'), array($arg1, $arg2)); consider this scenario: $this->object->method($arg1,
Consider this one piece of Perl code, $array[$x]->{foo}->[0]= January; I analyze this code as
Consider this JavaScript code snippet: Object.prototype.log = function() { // here, you have a
Consider this example: #include <iostream> #include <string> #include <vector> #include <iterator> int main() {

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.