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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:01:41+00:00 2026-05-16T05:01:41+00:00

I have a templated C++ class that exposes a number of methods, e.g template<int

  • 0

I have a templated C++ class that exposes a number of methods, e.g

template<int X, int Y>
class MyBuffer {
public:
    MyBuffer<X,Y> method1();
};

Now, I want to expose additional methods to this class if X == Y. I have done this by subclassing MyBuffer,

template<int X>
class MyRegularBuffer : public MyBuffer<X,X> {
public:
    MyRegularBuffer method2();
};

Now, the problem is that I want to be able to do e.g.

MyRegularBuffer<2> buf = ...
MyRegularBuffer<2> otherBuf = buf.method1().method2();

But I am not sure how to accomplish this. I tried to think of copy constructors, conversion operators, etc, but my C++ skills are unfortunately a bit rusty.

EDIT: I should add that creation of these objects is relatively cheap (and also, it won’t happen a lot), which means it would be OK to do something like this:

MyRegularBuffer<2> buf = ...
MyRegularBuffer<2> temp = buf.method1(); // Implicit conversion
MyRegularBuffer<2> otherBuf = temp.method2();

The question is then, how can I define the conversion like that. The conversion operator needs to be in MyBuffer, I think, but I want it to be available only if X==Y.

  • 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-16T05:01:41+00:00Added an answer on May 16, 2026 at 5:01 am

    You don’t need a separate class to represent the special behaviour. Partial specialization allows you to treat some of the MyBuffer <X,Y> cases specially and give them extra methods.

    Keep your original declaration of MyBuffer<X,Y> and add this:

    template<int Y>
    class MyBuffer<Y, Y> {
    public:
        MyBuffer<Y,Y> method1();
        MyBuffer<Y,Y> method2();
    };
    
    MyBuffer<1,2> m12; m12.method2(); // compile fail, as desired, as it doesn't have such a method because 1 != 2
    MyBuffer<2,2> m22; m22.method2(); // compile success
    

    Edit: my final lines weren’t very useful after all, as pointed out by Georg in the comments, so I’ve deleted them.

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

Sidebar

Related Questions

I have a templated class that I want to avoid copying (because of the
I have a templated container class that looks something like this: template <class ItemType>
I want to have a template class that looks something like what I have
I have code that looks like this: template<class T> class list { public: class
I have a templated class that is dependent on two template parameters to calculate
I have a templated class, that can have a map type as template parameter.
I have my templated container class that looks like this: template< class KeyType, class
I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I have a custom iterator template class that wraps up a std::list iterator. In
I have a template class with a variadic template member function that I am

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.