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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:29:39+00:00 2026-06-12T02:29:39+00:00

Given this code: //header.h template <class T> class Foo { public: Foo(T t) :

  • 0

Given this code:

//header.h
template <class T>
class Foo
{
public:
  Foo(T t) : t(t) {}
  T t;
};

//source1.cpp:
#include "header.h"
extern template class Foo<int>;
int main()
{
  Foo<int> f(42);
}

By my understanding, this program should not link, since there should be no definition of class Foo<int> anywhere (extern template should prevent this). With VC++ 11 (Visual Studio 2012), this does however compile and link. In GCC, it doesn’t:

source1.cpp:(.text+0x15): undefined reference to `Foo<int>::Foo(int)'

If I link with source2.cpp however, it works (as I expect i should) :

#include "header.h"
template class Foo<int>;

According to this blog post, extern template should have been supported since VC10.
http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx

On a side note, is there a way to list the names in an object file on Windows / Visual Studio? On Linux I would do:

$ nm source1.o
U _ZN3FooIiEC1Ei      <- "U" means that this symbol is undefined.
0000000000000000 T main
  • 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-12T02:29:41+00:00Added an answer on June 12, 2026 at 2:29 am

    C++11 14.7.2/10 “Explicit instantiation” says:

    Except for inline functions and class template specializations,
    explicit instantiation declarations have the effect of suppressing the
    implicit instantiation of the entity to which they refer.

    And the constructor in you class template Foo<T> is inline. VS2012 will work the way that you expect if you structure the header like so:

    //header.h
    template <class T>
    class Foo
    {
    public:
      Foo(T t);
      T t;
    };
    
    template <class T>
    Foo<T>::Foo(T t) : t(t) 
    {
    }
    

    so that the constructor isn’t inline.

    The paragraph from the standard I quoted above does include the following note:

    [ Note: The intent is that an inline function that is the subject of
    an explicit instantiation declaration will still be implicitly
    instantiated when odr-used (3.2) so that the body can be considered
    for inlining, but that no out-of-line copy of the inline function
    would be generated in the translation unit. — end note ]

    Looking at the assembly code created when the ctor is inlined, an out-of-line copy of the ctor is placed in the object file (even though the ctor is never even called if you compile the example with optimizations on), so MSVC doesn’t appear to be following the intent of the standard. However, notes are not normative, so I believe that MSVC’s behavior is conforming.


    Regarding your side question about dumping symbols from object files built with MSVC, you can use the dumpbin utility:

    When compiling the example with the non-inline constructor:

    dumpbin /symbols test.obj
    
    ...
    
    008 00000000 UNDEF  notype ()    External     | ??0?$Foo@H@@QAE@H@Z (public: __thiscall Foo<int>::Foo<int>(int))
                 ^^^^^
    ...
    

    Compiling the example with the ctor inlined:

    00A 00000000 SECT4  notype ()    External     | ??0?$Foo@H@@QAE@H@Z (public: __thiscall Foo<int>::Foo<int>(int))
                 ^^^^^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
Given this code sample: complex.h : #ifndef COMPLEX_H #define COMPLEX_H #include <iostream> class Complex
Given class Foo template <typename T> class Foo { public: ...other methods.. void bar()
I was looking over some (C++) code and found something like this: //Foo.cpp namespace
I have this code: <?php include 'connect.inc.php'; include 'header.inc.php'; $mesiace = array(Január, Február, Marec,
Given this code: List<Integer> ints = new ArrayList<Integer>(); // Type mismatch: // cannot convert
Given this code, tableButton = new JButton(new ImageIcon(80F_FG2015.GIF)); how would I get that String
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
Given this code: List<string> things = new List<string>(); foreach (string thing in things) {
So I was given this code in a folder of bits and pieces that

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.