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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:32:08+00:00 2026-05-22T22:32:08+00:00

I have a question about template specialization in C++, and I am hoping someone

  • 0

I have a question about template specialization in C++, and I am hoping someone here can help. I have a class that has 3 template parameters:

template<class A, class B, class C>
class myClass {

public:
  void myFunc();
};

What I want to do is write several versions of myFunc that specialize on, say, type C, but are generic for types A and B. So I do NOT want the fully templated function like this:

template<class A, class B, class C>
void myClass<A, B, C>::myFunc()
{
  // function code here
}

and I do NOT want a fully specialized function like this

void myClass<int, int, int>::myFunc()
{
  // code goes here
}

Instead, I want to do something that would be similar to

template<class A, class B>
void myClass<A, B, int>::myFunc()
{
  // code goes here
}

The idea is that if class type C is int, I would call one version of myFunc(), and if class type C is double, I would call a different version of myFunc. I’ve tried lots of difference combinations of template specialization syntaxes (too many to list here), and none seems to compile.

Could someone possibly point me in the right direction here? Thanks in advance for your help.

Michael

  • 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-22T22:32:08+00:00Added an answer on May 22, 2026 at 10:32 pm

    You can write a function template, and an overload, and delegate the work to it:

    template<class A, class B, class C>
    class myClass 
    {
       //resolver doesn't need to define anything in it!
       template<class> struct resolver {}; //empty, yet powerful!
    public:
      void myFunc() 
      {
           doFun(resolver<C>());
      }
    
      //this is a function template
      template<typename X>
      void doFun(const resolver<X> & )
      {
          //this function will get executed when C is other than int
          //so write your code here, for the general case
      }
    
      //this is an overload, not a specialization of the above function template!
      void doFun(const resolver<int> & ) 
      {
          //this function will get executed when C = int
          //so write your code here, for the special case when C = int
      }
    };
    

    Note an important point : doFun(const resolve<int>& ) is an overloaded function, its not a specialization of the function template. You cannot specialize member function template without specializing the enclosing class template.

    Read these articles:

    • Template Specialization and Overloading
    • Why Not Specialize Function Templates?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about templates that can be used with parameters that are
I have a question about the way .replace() works I know that we can
I have a question about template classes. For example, take this class template<class TBase>
I have a question about the calendar class. I can populate days in the
There are many question about template engines. Smarty is most popular. I have seen
I am coming here, because I have a question about Django and Thread. I
I have a question about a code that I have recently took in my
I have a random question about const correctness. Lets say i have a class
I have a question about inheritance and template methods. Suppose I have this two
I have general question about designated initializer. I have a some class and from

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.