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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:29:47+00:00 2026-06-05T18:29:47+00:00

I want to create a template class (let’s call it Foo ) that only

  • 0

I want to create a template class (let’s call it Foo) that only accepts a few specific type parameters (let’s say only double and float). Normally templates are implemented in the header file (.h) because it is unknown how it will be instantiated in user code. In this case, it makes more sense to implement the class in the implementation file (.cpp) like so:

// Foo.cpp:

template <class T>
class Foo
{
    // Insert members here
};

typedef Foo<double> Foo_d;
typedef Foo<float> Foo_f;

This would instantiate and compile the class when Foo.cpp is compiled. But then how to I declare this in the header file without writing separate declarations for Foo_d and Foo_f?

  • 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-05T18:29:48+00:00Added an answer on June 5, 2026 at 6:29 pm

    You can define the template in your header file, declaring the methods, but without defining them. For example:

    template <typename T>
    class Foo {
        T val;
    public:
        Foo (T t);
        T value ();
    };
    
    typedef Foo<double> Foo_d;
    typedef Foo<float> Foo_f;
    

    In the .cpp file, you complete the implementations of the methods, and then instantiate the templates that you want.

    #include "foo_template.hpp"
    
    template <typename T>
    Foo<T>::Foo (T t) : val(t) {}
    
    template <typename T>
    T Foo<T>::value () { return val; }
    
    template class Foo<double>;
    template class Foo<float>;
    

    The object file should have instantiations for Foo_d and Foo_f from the explicit instantiations of template Foo. Any other type used for template Foo will result in a linking error, since instantiations for them won’t exist. Or, more pedantically, the compiler creates instantiations on demand as usual, but it won’t be able to resolve the symbols corresponding to the methods of the class, because explicit instantiations for those won’t exist.

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

Sidebar

Related Questions

I want to create a template class in C#, for example: public class Foo<T>
I want to create a template for a component that is used many times
I want to create a Visual Studio Item Template that generates an Item (for
Let's imagine we have several type of elements, and we want to create a
I am writing a memory-managing template class in which I want to create a
How can i add pass Model-Specific urls to the Template. Let's say, i want
I want to create my own email template without having to use third party
i want create a counter that retrieve the number of month, day, hour, minute
I want create an array in JSF EL. How can I do that? Is
I have a C++ template class that contains a method pointer and a class

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.