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

  • Home
  • SEARCH
  • 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 9048103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:04:24+00:00 2026-06-16T12:04:24+00:00

Consider the following header and source files: // main.cpp #include myClass.h int main() {

  • 0

Consider the following header and source files:

// main.cpp
#include "myClass.h"

int main()
{
  MyClass m;
  m.foo<double>();
  m.foo<float>();
}

// myClass.h
#pragma once

#include <iostream>

using namespace std;

class MyClass
{
public:

  template <typename T>
  void foo()
  {
    cout << "Template function<T> called" << endl;
  }

  template <>
  void foo<int>()
  {
    cout << "Template function<int> called" << endl;
  }

  template <>
  void foo<float>();

};

// myClass.cpp
#include "myClass.h"

template <>
void MyClass::foo<float>()
{
  cout << "Template function<float> called" << endl;
}

I get a linking error wrt the foo<float> specialization. If I place the definition of the specialization in the header file, then everything works as expected.

I figured that the reason may be that the method is not explicitly instantiated (although full specialization of template class does not need an explicit instantiation for proper linking). If I try to explicitly instantiate the method, I get this error:

error C3416: ‘MyClass::foo’ : an explicit specialization may not be explicitly instantiated

So the questions are:

  • Is there any way to define the specialization in the cpp file and link properly?
  • If not, why not? I can explicitly instantiate template methods that
    are not specialized just fine. Why not the same for full specializations?
  • 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-16T12:04:25+00:00Added an answer on June 16, 2026 at 12:04 pm

    Although WhozCraig’s answer (now deleted) provides the correct code to solve your problem, here are some direct answers to your questions, including comments on your code:

    1. foo<int>() and foo<float>() are explicit specializations of a member template. Those must not appear inside the definition of the class they belong to. The Standard says:

      (§14.7.3/3) […] The definition of a class or class template shall precede the declaration of an explicit specialization for a member template of the class or class template. […]

      So you must put them after the class definition.

    2. In the case of foo<int>, which is fully defined in the header file, this implies that you must put the word inline before the definition; otherwise you will get into trouble with the linker if the header file is included in more than one translation unit.

    3. The specialization for foo<float>() is defined in a separate file that is later linked to main.cpp. This is possible, but it requires that a declaration of it be given in the header file (you do this already, but you must do it outside the class definition):

        template <>
        void MyClass::foo<float>();
      

      This is required because of another statement in the Standard:

      (§14.7.3/6) If a template, a member template or a member of a class template is explicitly specialized then that specialization shall be declared before the first use of that specialization that would cause an implicit instantiation to take place, in every translation unit in which such a use occurs; no diagnostic is required. […]

    4. Since all specializations are explicit (i.e. full specializations, to use your word), there is no need for explicit instantiations, but they are possible (§14.7.2). You would place them at the end of the .cpp file, and the syntax would be:

      template void MyClass::foo<float>();
      template void MyClass::foo<int>();
      

      Again, this is only really useful for types that do not have their own explicit specializations.

    The correct code for the header and implementation file therefore looks like this:

    .h file:

    class MyClass
    {
    public:
      template <typename T> void foo()
      { cout << "Template function<T> called" << endl; }
    };
    
    template <> inline void MyClass::foo<int>()
    { cout << "Template function<int> called" << endl; }
    
    template <> void MyClass::foo<float>();
    

    .cpp:

    #include "myClass.h"
    
    template <> void MyClass::foo<float>()
    { cout << "Template function<float> called" << endl; }
    
    /* This is unnecessary for float, but may be useful for
       types that do not have their own explicit specializations: */
    template void MyClass::foo<float>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider having the following header file (c++): myclass.hpp #ifndef MYCLASSHPP_ #define MYCLASSHPP_ namespace A
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider the following simple header, demo.h: #define PERSIST struct Serialised { int someTransientValue ;
Consider the following header file example: shared_example.h #ifndef SHARED_EX #define SHARED_EX const int Shared_Int
Consider the following example. It consists of two header files, declaring two different namespaces:
Following the instructions at Add header (copyright) information to existing source files , I
Consider the following header file: template <typename T> struct tNode { T Data; //the
Consider the following struct defined in ModuleA: typedef struct{ int A; int B; int
Consider the following header file, there are 2 Delegate Protocols declaration : NSSpeechSynthesizerDelegate and
Consider the following code: Here is my price calculator controller header file. #import <Foundation/Foundation.h>

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.