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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:47:34+00:00 2026-05-11T03:47:34+00:00

To specialize a class template, one has to redefine all of the member functions

  • 0

To specialize a class template, one has to redefine all of the member functions in the underlying base template (i.e. the unspecialized class template) even if they are expected to remain mostly unchanged. What are some of the accepted methods and ‘best practices’ to avoid this code duplication?

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. 2026-05-11T03:47:34+00:00Added an answer on May 11, 2026 at 3:47 am

    You can fully specialize a member selectively:

    template<int N> struct Vector {     int calculate() { return N; } };  // put into the .cpp file, or make inline! template<> int Vector<3>::calculate() { return -1; } 

    You do a full specialization. Meaning you cannot partial specialize it:

    template<int N, int P> struct Vector {     int calculate() { return N; } };  // WROOONG! template<int N> int Vector<N, 3>::calculate() { return -1; } 

    If you need that, you can use enable_if:

    template<int N, int P> struct Vector {      int calculate() { return calculate<P>(); } private:     // enable for P1 == 3     template<int P1>     typename enable_if_c<P1 == P && P1 == 3, int>::type     calculate() { return -1; }      // disable for P1 == 3     template<int P1>     typename enable_if_c<!(P1 == P && P1 == 3), int>::type     calculate() { return N; } }; 

    An alternative approach is to split your stuff up (common stuff into a base class, and specialized stuff into the derived class) like Nick recommends.

    I usually would take the second approach. But i prefer the first one if i don’t need to partial specialize the functions.

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

Sidebar

Related Questions

I'm trying to specialize just one method of a class template . The accepted
Is it possible to specialize this template for any basic_string's? template<class T> struct X
I want to specialize a class template with the following function: template <typename T>
I had defined two overloaded functions, their declarations are as follows template <class T>
The following code, which attempts to specialize class template 'special', based on the return
One of my classes declares a templated function: template<class A, class B> A do_something(const
I am investigating a problem with C++ class templates. One class template is specialized
I know from this thread - template class member function only specialization that if
I have a templated class with an templated member function template<class T> class A
Is it possible to specialize a inner class declared within a template class? More

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.