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

  • Home
  • SEARCH
  • 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 9110567
In Process

The Archive Base Latest Questions

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

I have a class Foo which uses CRTP to inherit a template method from

  • 0

I have a class Foo which uses CRTP to inherit a template method from a parent class and avoid having to provide literally dozens of individual member methods. Something like this:

class Foo : public SomeBarClass<Foo>
{
//..
//from SomeBarClass
public:
    template <class T> void onMsg(T* msg);

private:
    IFoxMod* foxMod_;
};

Now, in the implementation for onMsg, I would like something like this:

template <class T>
void Foo::onMsg(T* msg)
{
    if (foxMod_->shouldDoStuff(msg))
    {
        //do stuff
    }
}

and there can be many foxMod_ types (one of them instantiated in the Foo constructor by name given in config file) as long as they abide by the common interface of providing a bool shouldDoStuff method. The problem, is that this leads me to define the following:

struct IFoxMod
{
    virtual ~IFoxMod() {}
    template <class T> shouldDoStuff(T* msg) = 0;
};

for all of the FoxMods to implement (like, class redMountainLogic : public IFoxMod might have it’s own way of discerning, when it is appropiate to do stuff).

This is illegal though because one cannot have virtual templates and I’m trying to find a workaround for it. Basically, I need to have dynamic dispatch, but the argument I am passing is a template. I can’t think of a workaround.

  • 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-17T03:18:04+00:00Added an answer on June 17, 2026 at 3:18 am

    Virtual function tables don’t seem to get along well with template specializations. Not too surprising. VFTs are generally based on declaration order, which doesn’t really exist with templates. One solution is to manually recreate VFTs.

    Here’s an example. It could probably be a little cleaner, but it works.

    #include<iostream>
    using namespace std;
    
    // Message.h
    
    template<int n>
    struct MessageByInt {
      typedef int Msg;
    };
    
    struct MessageOfHope {
      int a;
      int b;
      static const int id = 0;
    };
    template<> struct MessageByInt<MessageOfHope::id> { typedef MessageOfHope Msg; };
    
    struct MessageOfDoom {
      int b;
      int c;
      static const int id = 1;
    };
    template<> struct MessageByInt<MessageOfDoom::id> { typedef MessageOfDoom Msg; };
    
    const int nMessages = 2;
    
    // IFoxMod.h
    
    typedef bool(*callback)(void*);
    
    struct IFoxMod {
      callback vtable[nMessages];
      template<typename MSG>
      bool ShouldDoWork(MSG* msg) {
        return vtable[MSG::id](msg);
      }
    };
    
    template<typename TESTER, int n>
    struct filler {
      typedef typename MessageByInt<n>::Msg MSG;
      typedef typename TESTER::template Tester<MSG> Tester;
      static void fill(IFoxMod* impl) {
        impl->vtable[n] = reinterpret_cast<callback>(&Tester::ReallyShouldDoWork);
        filler<TESTER,n-1>::fill(impl);
      }
    };
    
    template<typename TESTER>
    struct filler<TESTER,-1>{
      static void fill(IFoxMod* impl) {
      }
    };
    
    // RedFox.h
    
    struct RedFoxTester {
      template<typename MSG>
      struct Tester { // This struct exists to allow partial specialization
        static bool ReallyShouldDoWork(MSG* msg) {
          return msg->b == 2;
        }
      };
    };
    
    struct RedFoxMod : public IFoxMod {
      RedFoxMod() {
        filler<RedFoxTester,nMessages-1>::fill(this);
      }
    };
    
    //Main
    
    main() {
      IFoxMod* fm = new RedFoxMod();
      MessageOfHope mohb2 = {1, 2};
      MessageOfDoom modb2 = {2, 3};
      MessageOfHope mohbn2 = {2, 3};
      MessageOfDoom modbn2 = {1, 2};
      cout << fm->ShouldDoWork(&mohb2) << ", " << fm->ShouldDoWork(&modb2) << endl;
      cout << fm->ShouldDoWork(&mohbn2) << ", " << fm->ShouldDoWork(&modbn2) << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using MooseX::Declare and methods, which uses MooseX::Method::Signatures. Let's say I have a class
Let's say we have a class foo which has a private instance variable bar
Imagine I have a C++ class Foo and a class Bar which has to
I have a script which similar to this: foo.php class Foo { function Foo()
I have made a front end for a program which test java (foo.class) binary
I have a class Foo that uses another class Bar to do some stuff.
I have solution A which defines public class Foo, and solution B which references
I have a template method that needs to instantiate class objects with given class
I have 2 assemblies, A containing the Main method and the Foo class, that
I have a class which has a group of integers, say foo() { int

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.