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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:28:37+00:00 2026-05-17T20:28:37+00:00

If I compile the following code: // // g++ static.cpp -o static.o // ar

  • 0

If I compile the following code:

//
// g++ static.cpp -o static.o
// ar rcs libstatic.a static.o
//
#include <iostream>

template < typename T >
struct TemplatedClass
{
  void Test( T value )
  {
    std::cout << "Foobar was: " << value << std::endl;
  }
};

template struct TemplatedClass < long >;

I get a static library and if I run nm on the library I get the following results:

testcase% nm libstatic.a | c++filt | grep TemplatedClass
0000000000000207 s global constructors keyed to _ZN14TemplatedClassIlE4TestEl
0000000000000300 s global constructors keyed to _ZN14TemplatedClassIlE4TestEl.eh
0000000000000118 T TemplatedClass<long>::Test(long)
00000000000002a0 S __ZN14TemplatedClassIlE4TestEl.eh

If however I compile the following code, which is the same except that I have added an explicit specialization of the templated class…

//
// g++ static.cpp -o static.o
// ar rcs libstatic.a static.o
//
#include <iostream>

template < typename T >
struct TemplatedClass
{
  void Test( T value )
  {
    std::cout << "Foobar was: " << value << std::endl;
  }
};

template <>
struct TemplatedClass < long >
{
  void Test( long value )
  {
     std::cout << "Value was: " << value << std::endl;
  }
}; 

template struct TemplatedClass < long >;

… and rerun the same command:

testcase% nm libstatic.a | c++filt| grep TemplatedClass
testcase% 

I get no matching symbols. For some reason the compiler does not instantiate the template even though I explicitly asked it to.

Can someone explain to me what is going on here?

  • 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-17T20:28:37+00:00Added an answer on May 17, 2026 at 8:28 pm

    You have member function definitions within class (template) definitions. This causes the member functions (templates) to be inline. That doesn’t matter so much for the member function of the template class, since its linkage requirements are determined more by the nature of its instantiation(s).

    But in the second example, the member function void TemplatedClass<long>::Test(long) is not a function template and is still inline. So the compiler is not required to do anything with it unless it’s used, and it must be defined in all files where it’s used. Since you claim this is in a static.cpp file, an inline function is probably not what you want.

    I think you’ll get results more like you’re expecting if you change things to:

    template <>
    struct TemplatedClass < long >
    {
      void Test( long value );
    };
    
    void TemplatedClass<long>::Test( long value )
    {
      std::cout << "Value was: " << value << std::endl;
    }
    

    And when you define an explicit specialization, you probably don’t also need an explicit instantiation (if that’s even legal).

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

Sidebar

Related Questions

The following code does not compile: public class GenericsTest { public static void main(String[]
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
I am trying to compile the following code: private String dataToString(){ Map data =
The following code fails to compile stating A local variable named 'st' cannot be
The following Code does not compile Dim BasicGroups As String() = New String() {Node1,
This following code gets this compile error: " invalid types 'int[int]' for array subscript
I want to compile the following line of code from http://code.google.com/p/enhsim : enh::eout <<
I have the following code that won't compile and although there is a way
Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
Why whenever I compile and run the following code in Visual Studio 2008: double

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.