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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:19:45+00:00 2026-06-16T17:19:45+00:00

Is this a correct usage of extern template in C++11? (Can it be that

  • 0

Is this a correct usage of extern template in C++11? (Can it be that the extern template class and respective template class is visible in the same translation unit?)

// example.hpp:
#pragma once
template< typename T >
class C {
    void f(T);
};
// question is about the next two lines
extern template class C< float >;
extern template class C< double >;
// example_def.hpp:
#include "example.hpp"
template< typename T >
void C< T >::f(T) {
    //... smth. practicable
}
// example.cpp:
#include "example_def.hpp"
template class C< float >;
template class C< double >;
// other.hpp:
#pragma once
void g();
// other.cpp:
#include "other.hpp"
#include "example.hpp"
// maybe those two lines should be here instead?
void g() {
    C< float >();
    C< double >();
}
// main.cpp:
#include "example.hpp"
#include "other.hpp"
// ...and here?
int main() {
    C< float >();
    C< double >();
    g();
    return 0;
}
  • 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-06-16T17:19:46+00:00Added an answer on June 16, 2026 at 5:19 pm

    Yes, both an extern template class specification (called explicit instantiation declaration by the Standard) and a template class specification (called explicit instantiation definition by the Standard) can be in the same translation unit, if the definition (without extern) follows the declaration (with extern):

    (§14.7.2/11) If an entity is the subject of both an explicit instantiation declaration and an explicit instantiation definition in the same translation unit, the definition shall follow the declaration. An entity that is the subject of an explicit instantiation declaration and that is also used in a way that would otherwise cause an implicit instantiation (14.7.1) in the translation unit shall be the subject of an explicit instantiation definition somewhere in the program; otherwise the program is ill-formed, no diagnostic required. [ Note: This rule does apply to inline functions even though an explicit instantiation declaration of such an entity has no other normative effect. This is needed to ensure that if the address of an inline function is taken in a translation unit in which the implementation chose to suppress the out-of-line body, another translation unit will supply the body. — end note ] An explicit instantiation declaration shall not name a specialization of a template with internal linkage.

    (Emphasis mine). The terms explicit instantiation declaration and explicit instantiation definition are defined here:

    (§14.7.2/2) The syntax for explicit instantiation is:

    explicit-instantiation:
    externopt template declaration

    There are two forms of explicit instantiation: an explicit instantiation definition and an explicit instantiation declaration. An explicit instantiation declaration begins with the extern keyword.


    The effect of these explicit instantiations is as follows:

    1. The explicit instantiation declaration (with extern) prevents all implicit instantiations to take effect (except for inline functions and class template specializations, §14.7.2/10).

    2. The explicit instantiation definition (without extern) causes the instantiation to happen no matter what, i.e. it overrides the explicit instantiation declaration (this also follows from §14.7.2/10).


    General comments
    The fact that your explicit instantiation declarations are located in the header file that defines the template implies that anyone who includes the header files in order to make use of the template will either have to also add an explicit instantiation definition, or, alternatively, needs to link to the code of another .cpp file that includes such an explicit instantiation definition.

    This can be confusing and is probably not a very good idea when you expect many different users to instantiate the template for many different types. But it can be sensible if the number of instantiations for distinct types is small and you can anticipate them all. Of course you must make sure that there is one (or several) .cpp file(s) that include explicit instantiation definitions for all the instantiations required, and that its corresponding object file is linked with the project at build time.

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

Sidebar

Related Questions

In some unit/integration tests of the code we wish to check that correct usage
In PHP, filter_var('www.example.com', FILTER_VALIDATE_URL) returns false . Is this correct? Isn't www.example.com a valid
Is this the correct way to do it? <a id=load href=# class=btn load onclick=request(this);
I'm doing something similar to this item Correct BOOST_FOREACH usage? However, my returned list
What is the correct usage pattern of HTablePool? I mean, assume that I have
I may be misunderstanding the correct usage of this function, but I believe appendChild
I don't know if I have got this correct but what I am trying
What do you think? Is this correct or are there memory leaks? Source: #include
I'm confused with bridge and bridge_transfer , is this correct? -(void)getData{ ABAddressBookRef addressBook =
Is this Regex correct if I have to match a string which is atleast

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.