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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:18:04+00:00 2026-06-17T17:18:04+00:00

I have implemented a Policy using the CRTP. The policy requires the Base class

  • 0

I have implemented a Policy using the CRTP. The policy requires the Base class to have a function called foo:

template<typename Base>
struct Policy<Base> {
  // ...
  Base* b(){ return static_cast<Base*>(this); }
  void do(){ b()->foo(); }
};

I have one class called Widget that uses my policy. Widget implements foo and everything is fine:

struct Widget : Policy<Widget> {
  // ...
  void foo();
};

The problem: I also have a type called OldWidget that implements the functionality of foo in a function named oldFoo:

struct OldWidget : Policy<OldWidget> {
  // ...
  void oldFoo();
};

I don’t want to modify OldWidget (besides extending it with the policy). I don’t want to use an AdaptedOldWidget:

struct AdaptedOldWidget : OldWidget, Policy<AdaptedOldWidget> {
  void foo(){ oldFoo(); }
};

The best would be to extend my existing policy_traits class to something like:

template<typename T>
struct policy_traits {};

template<>
struct policy_traits<Widget> {
  // typedefs...
  member_function_name = foo;
};

template<>
struct policy_traits<OldWidget> {
  // typedefs
  member_function_name = oldFoo;
};

Such that I can implement the Policy like this:

template<typename Base>
struct Policy<Base> {
  // ...
  Base* b() { return static_cast<Base*>(this); }
  void do(){ b()->policy_traits<Base>::member_function_name(); }
};

Is there away to achieve something like this in C++?

Proposed solution: I could do the following:

template<typename Base>
struct Policy<Base> : Policy_Member_Traits<Base> {
  // ...
  Base* b(){ return static_cast<Base*>(this); }
  void do(){ foo_wrapper(); }
};

template<typename T> struct Policy_Member_Traits { };
template<> struct Policy_Member_Traits<Widget> { 
  void foo_wrapper(){ static_cast<T*>(this)->foo(); }
};
template<> struct Policy_Member_Traits<OldWidget> { 
  void foo_wrapper(){ static_cast<T*>(this)->oldFoo(); }
};

There must be hopefully a better easier way to achieve this.

  • 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-17T17:18:05+00:00Added an answer on June 17, 2026 at 5:18 pm

    Here’s an example how specializing selectively. First, some example classes:

    #include <iostream>
    
    struct Foo
    {
        void foo() const { std::cout << "Foo::foo\n"; }
        void bar() const { std::cout << "Foo::foo\n"; }
    };
    
    struct Biz
    {
        void old_foo() const { std::cout << "Fiz::old_foo\n"; }
        void bar() const { std::cout << "Fiz::foo\n"; }
    };
    
    struct Fiz
    {
        void foo() const { std::cout << "Biz::foo\n"; }
        void old_bar() const { std::cout << "Biz::old_foo\n"; }
    };
    

    Now the trait:

    template <typename T> struct Dispatch
    {
        static void foo(T const & x) { x.foo(); }
        static void bar(T const & x) { x.bar(); }
    };
    
    template <> void Dispatch<Biz>::foo(Biz const & x) { x.old_foo(); }
    template <> void Dispatch<Fiz>::bar(Fiz const & x) { x.old_bar(); }
    

    And here’s a usage example:

    template <typename T> void dispatch(T const & x)
    {
        Dispatch<T>::foo(x);
        Dispatch<T>::bar(x);
    }
    
    int main()
    {
        Foo f;
        Biz b;
        Fiz c;
    
        dispatch(f);
        dispatch(b);
        dispatch(c);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have that template class that uses a policy for it's output and another
Introduction Peter Weinhart describes how to design a generic intrusive_ptr base class using CRTP
I have implemented clean URLs using the following in my .htaccess RewriteEngine on RewriteCond
I have implemented a very basic sign up using email address+name, although I would
I am using ActiveMQ 5.7.0 and trying to implement a redelivery policy. I have
I have a one time password system implemented for my website using RFC 4226
We have implemented a simple chat room feature in Rails using Simple Ajax updates.
I have implemented a Webservice using Apache CXF . The flow is this way
Is there any other policy that any of you have implemented other than the
I have implemented correctly bump's api, and added this code: - (void) configureBump {

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.