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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:24:50+00:00 2026-06-12T16:24:50+00:00

Example 1 If we have Base and Derived classes class Base {}; class Derived

  • 0

Example 1

If we have Base and Derived classes

class Base
{};
class Derived : public Base
{};

and a template class

template <class T, class Enabler=void>
class Partialy
{
public:
    void say()
    {
        std::cout << "Partialy Default" << std::endl;
    }
};

We can write a partial specialization next way:

template <class T>
class Partialy<T, typename std::enable_if<std::is_base_of< Base, T >::value>::type>
{
public:
    void say()
    {
        std::cout << "Partialy special" << std::endl;
    }
};

Example 1 will work OK. (Full code: http://ideone.com/4FyrD )

Example 2

If only I make Base class template one:

template <class T>
class BaseTpl
{};
class DerivedTpl : public BaseTpl<int>
{};

The next partial specializetion don’t work:

template <class T, class Ta>
class Partialy<T, typename std::enable_if<std::is_base_of< BaseTpl<Ta>, T >::value>::type>
{
public:
    void say()
    {
        std::cout << "Partialy special with TPL" << std::endl;
    }
};

Example 2 won’t compile
The compiler throws an error "template parameters not used in partial specialization:" (Full code: http://ideone.com/gZ6J2 )

Question

So the question is. Is there any way to write partial specializaion which would work for all classes which are derived from BaseTpl.

The list of classes for which the specialization shold work:

class A1 : public BaseTpl<int>
class A2 : public BaseTpl<std::string>
class A3 : public BaseTpl<vector<int> >
...
  • 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-12T16:24:51+00:00Added an answer on June 12, 2026 at 4:24 pm

    The way you have it, it is impossible for the compiler to match the Ta against any type. Imagine you had this:

    class DerivedTpl : public BaseTpl<int>, public BaseTpl<char>
    {};
    

    What type will Ta be when it tries to match?

    For your particular problem, one solution is to introduce an all-encompassing base class above BaseTpl:

    class BaseNonTpl {};
    
    template <class T>
    class BaseTpl : BaseNonTpl
    {};
    

    Then, modify the partial specialisation to look for that base class instead.

    template <class T>
    class PartialyTpl<T, typename std::enable_if<std::is_base_of< BaseNonTpl, T >::value>::type>
    

    That way, the compiler doesn’t have to try and figure out what base class you meant to use.

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

Sidebar

Related Questions

For example: I have an implementation (say doSomething()) in Base class. I want all
I have a base class and several derived classes. The derived classes use some
Say we have a class inheriting from two base classes (multiple inheritance). Base class
I have a template base class like this: template<typename T, std::size_t Size> class VectorT
I have a base class IStructure which is derived by many classes. Some of
I have an abstract base class from which many classes are derived. I want
I have an abstract base class that enforces certain operations on all derived classes.
I have a base.html template that contains a list of links. Example: <div id=sidebar1>
I have a model, for example : class Account < ActiveRecord::Base before_create :build_dependencies def
Example I have a repository class (DAL): public class MyRepository : IMyRepository { public

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.