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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:28:43+00:00 2026-06-16T10:28:43+00:00

I have a template class: template<typename T> class A { public: virtual void func(T

  • 0

I have a template class:

template<typename T>
class A {
public:
 virtual void func(T t);
 virtual void func2(T t);
 .
 .
 .
 virtual void funcN(T t);
}

Basically a lot of classes inherit form A, but T is always one of the 2 types B or C.

Currently every change that i make in the class causes a very long build.

Since T can be either B or C i want to turn the class into a regular class.

Any ideas how to do this change in a smart and clean way, without copying each function twice?

Thanks

  • 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-16T10:28:45+00:00Added an answer on June 16, 2026 at 10:28 am

    The easiest solution is probably to use explicit specialisations. You keep the template, but you tell the compiler explicitly for which classes to instantiate it.

    Change your header file containing class A to contain only the declaration of the class and its members (like you would do for a regular class). Then create a file A.cpp with the implementation of the member-functions of A and the explicit specialisations, like this

    #include <A.h>
    #include <B.h>
    #include <C.h>
    
    //... Implementation of A
    
    template class A<B>; // explicit instantiation for B
    template class A<C>; // and for C
    

    and add this file to your project.

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

Sidebar

Related Questions

Let's say i have following class: template <typename T> class CModule{ public: virtual void
I have the following scenario :- template <typename T> class Base { public: virtual
Suppose a construct like this: class Interface { public: template <typename T> virtual void
I have a class template <typename Iterator, typename Value> class Foo { public: Foo(const
I have a class similar to the following: class SomeClass { public: template<typename... Args>
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
I have this class: template<typename T> class Parser { public: Parser() : count(0) {}
I have Derived Classes that inherit from a Base class with virtual functions. Im
I have the following class: template <typename T> class matrix { private: int _n;
I have the following class definition: template<typename QueueItemT> class QueueBC { protected: QueueBC() {}

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.