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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:25:22+00:00 2026-05-28T15:25:22+00:00

I was (and have been for a long time) under the impression that you

  • 0

I was (and have been for a long time) under the impression that you had to fully define all template functions in your .h files to avoid multiple definition errors that occur due to the template compilation process (non C++11).

I was reading a co-worker’s code, and he had a non-template class that had a template function declared in it, and he separated the function declaration from the function definition (declared in H, defined in CPP). It compiles and works fine to my surprise.

Is there a difference between how a template function in a non template class is compiled, and how a function in a template class is compiled? Can someone explain what that difference is or where I might be confused?

  • 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-28T15:25:23+00:00Added an answer on May 28, 2026 at 3:25 pm

    The interesting bit is how and when the template gets instantiated. If the instantiations can be found at link time, the template definition doesn’t need to be visible in the header file.

    Sometimes, explicit instantiations are cause like this:

    • header :

      struct X { 
          // function template _declaration_
          template <typename T> void test(const T&);
      };
      
    • cpp:

      #include "X.h"
      
      // function template _definition_:
      template <typename T> 
          void X::test(const T&)
      {
      }
      
      // explicit function template _instantiation(s)_:
      template X::test<int>(const int&);    
      template X::test<std::string>(const std::string&);
      

    Using this sample, linking will succeed unless uninstantiated definitions of the template are used in other translation units

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

Sidebar

Related Questions

I have been using attachment_fu on a project for a long time and all
I've just finished merging two branches that have been separated for a long time,
I have long been of the opinion that the time and resources it would
This is a question that I have been pondering for a long time ,
I have been trying for a long time to get SQL Server Express on
I have been stuck for a long time on this simple but obtuse message
I have been asking myself this question for a long time now. Thought of
I have been in love with zsh for a long time, and more recently
I have been thinking of starting a site of my own for long time.
I'm a long-time C++ programmer developing on Windows, and have been using Visual Studio

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.