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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:08:05+00:00 2026-05-31T02:08:05+00:00

I have policies that derive from a base policy. Some classes specialize for the

  • 0

I have policies that derive from a base policy. Some classes specialize for the derived policies while others specialize only for the base policy and can work with all derived policies.

The problem I am running into is too much code-duplication (mainly constructors and some boiler plate code for the class itself). The code below may provide a better explanation of what I mean:

struct BasePolicy {};
struct DerivedPolicy1 : public BasePolicy {};
struct DerivedPolicy2 : public BasePolicy {};
//... more policies deriving from BasePolicy (or other BasePolicies)
struct AnotherPolicy {};

template <typename T>
struct Foo;

// This struct can work for all BasePolicy types which includes all derivations
// of it (but doesn't because it is specialized for BasePolicy only)
template<>
struct Foo<BasePolicy>
{
  //... many constructors along with code
};

template<>
struct Foo<AnotherPolicy>
{
  //... more code
};

/* Would like to avoid the following code as it duplicates the above when it 
   comes to constructors and other things such as typedefs */
//template<>
//struct Foo<DerivedPolicy1> : Foo<BasePolicy>
//{
//  //... same constructors as Foo<BasePolicy>
//};
//
//template<>
//struct Foo<DerivedPolicy2> : Foo<BasePolicy>
//{
//  //... same constructors as Foo<BasePolicy>
//};

int main()
{
  // would like this to compile without uncommenting the commented out code and
  // without having the client (i.e. the line below) to somehow get the base
  // type of the policy (although if it can be done transparently, that will
  // work)
  Foo<DerivedPolicy1> a; 
};

Is there any way for a derived policy to be accepted by a class specialized for the base policy? I would like the client to not do anything extra.

The following is not valid C++ code, but I would like something like this happen (if you keep the above code in mind):

template<>
struct Foo<BasePolicy | DerivedPolicy1 | DerivedPolicy2>
{
  //... many constructors along with code
};
  • 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-31T02:08:06+00:00Added an answer on May 31, 2026 at 2:08 am

    This is a case for SFINAE.

    template< typename Policy, ...some_condition... >
    struct Foo<Policy>
    {
     ...
    };
    

    You should decide what is some_condition exactly. You can specify that Policy derives from BasePolicy:

    template< typename Policy, enable_if< is_base<BasePolicy, Policy> > >
    

    Or you can list allowed policies explicitly:

    template< typename Policy,
               enable_if_c <is_same<Policy, BasePolicy>::value || 
                             is_same<Policy, DerivedPolicy1>::value> || 
                             ...whatever...
                            >
             >
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a module where all classes that implement my policies are defined. class
For a policy-based class-design I need some of the policies to call functions that
I have some pages that have content that is relevant to both logged in
I have a canvas in Flex that shall be able only to be scrolled
I have some confusion in understanding Java Security model. In my ${JDK_HOME}/jre/lib/security/java.policy file, I
I want to have user rights split into two. Some of the policies at
I have seen the thread pool executor implementation and the rejected execution policies that
I have a model that for edit/update actions only is logically split into two
In some project we have very that even our staff is not suppose to
I have trouble to find the kernel API to enable some qdisc policies tc

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.