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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:16:35+00:00 2026-05-30T10:16:35+00:00

I have a class structure which is template<int T> class MyClass { public: MyClass(){};

  • 0

I have a class structure which is

template<int T>
class MyClass {
public:
    MyClass(){};
    ~MyClass(){};

    template<class K> void foo();
};

Now I want to specialize the foo() method based upon the value of the integer used in MyClass<int>, for instance if int the code we have MyClass<2> I want to use a different version of foo<K>() to if I had MyClass<3>. However I want to still have foo<K>() unspecialized on K.

So that would mean something like this would be okay

MyClass<2> myc2;
MyClass<3> myc3;
myc2.foo<SomeClass>();
myc2.foo<SomeOtherClass>();
myc3.foo<SomeClass>();
myc3.foo<SomeOtherClass>();

Is it possible to specialize in this way but not specialize on K? I’ve tried a few combinations with no success.

  • 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-30T10:16:36+00:00Added an answer on May 30, 2026 at 10:16 am

    If I understood your question correctly, this is what you want:

    template<int T>
    class MyClass {
    public:
        MyClass(){};
        ~MyClass(){};
    
        template<class K> void foo();
    };
    
    template<int T>
    template<class K>
    void MyClass<T>::foo()
    {
        std::cout << "general form\n";
    }
    
    template<>
    template<class K>
    void MyClass<2>::foo()
    {
        std::cout << "MyClass<2>\n";
    }
    
    int main()
    {
        MyClass<3> c1;
        c1.foo<int>(); // general form
        MyClass<2> c2;
        c2.foo<int>(); // MyClass<2>
        c2.foo<float>(); // MyClass<2>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data structure which is as given below: class File { public
I have a business object project, which contains composite structure: public class Tree {
I have code structure in which I have class file, template file and controller
We have a structure like below: template<size_t size> class Container{ public: char characters[size]; };
I have a java class which fires custom java events. The structure of the
I have a class structure like abstract class Animal { public Animal(){ //init stuff..
I have the following class structure public class Outer{ private Mapper a; .... private
Let's say I have the following class structure: class Car; class FooCar : public
Say I have a class, something like the following; class MyClass { public: MyClass();
In this I need C++ array class template, which is fixed-size, stack-based and doesn't

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.