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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:35:21+00:00 2026-05-26T19:35:21+00:00

Figure 1: function templates TemplHeader.h template<typename T> void f(); TemplCpp.cpp template<typename T> void f(){

  • 0

Figure 1: function templates

TemplHeader.h

template<typename T>
void f();

TemplCpp.cpp

template<typename T>
void f(){
   //...
}    
//explicit instantation
template void f<T>();

Main.cpp

#include "TemplHeader.h"
extern template void f<T>(); //is this correct?
int main() {
    f<char>();
    return 0;
}

Is this the correct way to use extern template, or do I use this keyword only for class templates as in Figure 2?

Figure 2: class templates

TemplHeader.h

template<typename T>
class foo {
    T f();
};

TemplCpp.cpp

template<typename T>
void foo<T>::f() {
    //...
}
//explicit instantation
template class foo<int>;

Main.cpp

#include "TemplHeader.h"
extern template class foo<int>();
int main() {
    foo<int> test;
    return 0;
}

I know it is good to put all of this in one header file, but if we instantiate templates with the same parameters in multiple files, then we got multiple same definitions and the compiler will remove them all (except one) to avoid errors. How do I use extern template? Can we use it only for classes, or can we use it for functions too?

Also, Figure 1 and Figure 2 may be expanded to a solution where templates are in a single header file . In that case, we need to use the extern template keyword to avoid multiple same instantations. Is this only for classes or functions 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-26T19:35:22+00:00Added an answer on May 26, 2026 at 7:35 pm

    You should only use extern template to force the compiler to not instantiate a template when you know that it will be instantiated somewhere else. It is used to reduce compile time and object file size.

    For example:

    // header.h
    
    template<typename T>
    void ReallyBigFunction()
    {
        // Body
    }
    
    // source1.cpp
    
    #include "header.h"
    void something1()
    {
        ReallyBigFunction<int>();
    }
    
    // source2.cpp
    
    #include "header.h"
    void something2()
    {
        ReallyBigFunction<int>();
    }
    

    This will result in the following object files:

    source1.o
        void something1()
        void ReallyBigFunction<int>()    // Compiled first time
    
    source2.o
        void something2()
        void ReallyBigFunction<int>()    // Compiled second time
    

    If both files are linked together, one void ReallyBigFunction<int>() will be discarded, resulting in wasted compile time and object file size.

    To not waste compile time and object file size, there is an extern keyword which makes the compiler not compile a template function. You should use this if and only if you know it is used in the same binary somewhere else.

    Changing source2.cpp to:

    // source2.cpp
    
    #include "header.h"
    extern template void ReallyBigFunction<int>();
    void something2()
    {
        ReallyBigFunction<int>();
    }
    

    Will result in the following object files:

    source1.o
        void something1()
        void ReallyBigFunction<int>() // compiled just one time
    
    source2.o
        void something2()
        // No ReallyBigFunction<int> here because of the extern
    

    When both of these will be linked together, the second object file will just use the symbol from the first object file. No need for discard and no wasted compile time and object file size.

    This should only be used within a project, like in times when you use a template like vector<int> multiple times, you should use extern in all but one source file.

    This also applies to classes and function as one, and even template member functions.

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

Sidebar

Related Questions

I'm trying to figure out how to write this function: template <typename Bound> Bound::result_type
consider this simple and pointless code. #include <iostream> struct A { template<int N> void
This function of mine keeps on failing an autograder, I am trying to figure
Say I have the following template function: template <class T> void apply(const vector<complex<T> >&
I cannot figure out why this segment gives unresolved overloaded function error (gcc version
I can't figure out why I get this error : /usr/local/include/boost/asio/impl/read.hpp: In member function
I'm trying to figure out why this code $(document).ready(function() { $(.image2_template).effect( pulsate, {times:5}, 3000
I have written a template function like this: template <class T> const T& max1(const
I'm trying to figure how a function I've been given works -- or rather
Can I figure out if a function has already been assigned to an event?

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.