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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:47:28+00:00 2026-05-20T17:47:28+00:00

I am hoping someone can point out the correct way to specialize a method

  • 0

I am hoping someone can point out the correct way to specialize a method in a template class while using “extern template class” and “template class” for explicit instantiation with gnu c++. I’ve tried to boil down this problem with the simplest example that mimics my real problem. It appears that declaring “extern template” implies a template instantiation which causes errors when specializing methods. Given a driver program:

main.cc

#include A_H
#include <iostream>

int main()
{
    A<int> ai;
    A<long> al;

    std::cout << "ai=" << ai.get() << " al=" << al.get() << std::endl;

    return 0;
}

And the following implemntation of A

a.h

template<typename T>
struct A
{
    int get() const;
};

extern template class A<int>;
extern template class A<long>;

a.cc

#include "a.h"

template<typename T>
int A<T>::get() const
{
    return 0;
}

template<>
int A<long>::get() const
{
    return 1;
}

template class A<int>;
template class A<long>;

I receive the following error when compiling with either, g++ 4.1.2 or 4.4.4

 % g++ -Wall -g -D'A_H="a.h"' a.cc main.cc          
a.cc:10: error: specialization of 'int A<T>::get() const [with T = long int]' after instantiation
 %

If I comment out the two “extern template” lines in a.h, things compile and work as expected with both compilers. I assume depending on the existence of an explicit instantiation in the absence of “extern template” is unspecified behavior even in C++0x, otherwise, what’s the point of C++0x adding “extern template”?

If I instead implement A as:

a-hack.h

template<typename T>
struct A
{
    int get() const;
};

template<typename T>
int A<T>::get() const
{
    return 0;
}

template<>
inline
int A<long>::get() const
{
    return 1;
}

extern template class A<int>;
extern template class A<long>;

a-hack.cc

#include "a-hack.h"

template class A<int>;
template class A<long>;

and compile again, this works as expected

% g++ -Wall -g -D'A_H="a-hack.h"' a-hack.cc main.cc
% ./a.out 
ai=0 al=1

However, in my real world example, this causes a program crash with g++ 4.1.2 (while working for g++ 4.4.4). I have not narrowed down the exact cause of the crash (segmentation fault). It only appears as if the stack pointer is corrupted within what would be the call to A<>::get().

I realize that explicit template instantiation is non-standard at this point, but would anyone expect what I’ve done above to work? If not, what is the correct way to do this?

Thanks

  • 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-20T17:47:28+00:00Added an answer on May 20, 2026 at 5:47 pm
    extern template class A<long>;
    

    This line says that A<long> is to be explicitly instantiated according to the definitions the compiler has already seen. When you add a specialization later, you break that meaning.

    Add a declaration of your specialization to the header file.

    template <typename T> struct A { /*...*/ };
    template<> int A<long>::get() const;
    extern template class A<int>;
    extern template class A<long>;
    

    In general, it’s best to put as many specialization declarations as possible in the same header file as the primary template, to reduce surprises for the compiler about which declaration should be used for any particular instantiation.


    Notice that the extern template declaration isn’t necessary if you’re dealing with a single template entity (as opposed to this case, where we have to instruct the compiler about both the class A<long> and the function A<long>::get()). If you want to specialize a function template in another translation unit, it suffices to write just template<>.

    template<typename T> int freeGet() { return 0; }  // you can even add "inline" here safely!
    template<> int freeGet<long>();  // this function is not inline (14.7.3/12)
    

    But you must have the <> there. If you omit the <>, the declaration turns into an explicit instantiation of the default implementation (return 0), which is likely not what you wanted! Even if you add extern, the compiler is allowed to inline that default implementation; if your code unexpectedly breaks when you pass -O2, you might have accidentally omitted the <> somewhere.

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

Sidebar

Related Questions

I was hoping someone can point out what my problem could be. I am
Hoping this is something I can fix, or maybe someone can point out an
I got a little stuck and I'm hoping someone can point me in the
Bit of an odd question but I'm hoping someone can point me in the
I have just started using SlickGrid and was hoping someone can help me solve
Hoping someone can point me in the direction of where i'm going wrong with
UPDATED THANKS TO ANSWERS: Can someone point out the difference between: $ch = curl_init();
Hoping someone can provide an answer with this, although it's not 100% programming related.
I'm hoping someone can clarify this behavior for me, and explain how ASP.NET is
As a follow-up to this question I am hoping someone can help with the

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.