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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:06:34+00:00 2026-06-06T09:06:34+00:00

I realize that virtual template functions are not allowed in c++. Because of my

  • 0

I realize that virtual template functions are not allowed in c++. Because of my specific application domain, we deal with sets of algorithms (natural for implementation through polymorphism and inheritance) and need to enforce a common interface. Particular algorithmic classes work over iterators (not surprising), however we would like to fake virtualization through these templated functions. Here is an example of a solution we came up with using boost::mpl. I realize this is lengthy, but this is a minimal code example that I could create to simulate what I am aiming for. My specific question follows after the code.

#include <iostream>
#include <vector>
#include <boost/mpl/list.hpp>
#include <boost/mpl/for_each.hpp>

using namespace std;

class A;
class B;
class C;

typedef boost::mpl::list<B, C> DerivedClassList; 

template<typename Base, typename Iterator>
struct VirtualFunc{
  public:
    VirtualFunc(Base* _memory, Iterator _begin, Iterator _end) : 
      m_memory(_memory), m_begin(_begin), m_end(_end){}

    template<typename T>
      void operator()(T& _t) {
        T* tptr = dynamic_cast<T*>(m_memory);
        if(tptr != NULL){
          tptr->Print(m_begin, m_end);
        }   
      }   

  private:
    Base* m_memory;
    Iterator m_begin, m_end;
};  

class A{
  public:
    A(){}
    virtual ~A(){}

    template<typename Iterator>
      void Print(Iterator _begin, Iterator _end){
        boost::mpl::for_each<DerivedClassList>(VirtualFunc<A, Iterator>(this, _begin, _end));
      }   
};  

class B : public A {
  public:
    B(){}
    virtual ~B(){}

    template<typename Iterator>
      void Print(Iterator _begin, Iterator _end){
        cout << "Begin::" << *_begin << endl;
      }
};

class C : public A {
  public:
    C(){}
    virtual ~C(){}

    template<typename Iterator>
      void Print(Iterator _begin, Iterator _end){
        for(Iterator it = _begin; it!=_end; it++)
          cout << "Iterator::" << *it << endl;
      }
};

int main(){
  vector<size_t> numbers;
  for(size_t i = 0; i<5; i++)
    numbers.push_back(i);

  A* printBegin = new B();
  A* printAll = new C();
  //faking virtualism will print just begin
  printBegin->Print(numbers.begin(), numbers.end());
  //faking virtualism will print all
  printAll->Print(numbers.begin(), numbers.end());
}

So what is the pitfalls of this “fake virtual” templated functions? Is there a better more concise way to do this?

Also excuse the code standards, they are what we use at my workplace.

  • 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-06T09:06:35+00:00Added an answer on June 6, 2026 at 9:06 am

    Why not replace with classic double dispatch pattern. It seems that you know your class hierarchy at the base level – so I would use the following. It is well known, Visitor or DoubleDispatch pattern and eliminate the non efficient dynamic_cast. Frankly – if I see dynamic_cast<> I always think about double-dispatch,

    Known clasees:

    class A;
    class B;
    class C;
    

    Start point of virtual-ism:

    class IVirtualFunc {
      public:
        virtual void callFor(B& memory) = 0;
        virtual void callFor(C& memory) = 0;
    };  
    

    Implementation for the template argument:

    template<typename Iterator>
    class VirtualFunc : public IVirtualFunc {
      public:
        VirtualFunc (Iterator _begin, Iterator _end) : begin(_begin), end(_end) {}
        virtual void callFor(B& memory);
        virtual void callFor(C& memory);
      private:
        Iterator begin;
        Iterator end;   
    };
    

    The abstract base class for the actual implementations:

    class A{
      public:
        template<typename Iterator>
        void Print(Iterator _begin, Iterator _end) {
            VirtualFunc<Iterator> vFunc(_begin, _end);
            dispatch(vFunc);   
        }
        virtual void dispatch(IVirtualFunc&) = 0;   
    };  
    

    First actual implementation with double dispatch for it (VirtualFunc<Iterator>::callFor(B& b)):

    class B : public A {
      public:
        B(){}
        virtual ~B(){}
    
        template<typename Iterator>
          void Print(Iterator _begin, Iterator _end){
            cout << "Begin::" << *_begin << endl;
          }
        virtual void dispatch(IVirtualFunc& vf) { vf.callFor(*this); }   
    };
    
    template<typename Iterator>
    void VirtualFunc<Iterator>::callFor(B& b)
    {
         b.Print(begin, end);
    }
    

    Second actual implementation with double dispatch for it (VirtualFunc<Iterator>::callFor(C& c)):

    class C : public A {
      public:
        C(){}
        virtual ~C(){}
    
        template<typename Iterator>
          void Print(Iterator _begin, Iterator _end){
            for(Iterator it = _begin; it!=_end; it++)
              cout << "Iterator::" << *it << endl;
          }
        virtual void dispatch(IVirtualFunc& vf) { vf.callFor(*this); }   
    };
    template<typename Iterator>
    void VirtualFunc<Iterator>::callFor(C& c)
    {
         c.Print(begin, end);
    }
    

    And the proof it works:

    int main(){
      vector<size_t> numbers;
      for(size_t i = 0; i<5; i++)
        numbers.push_back(i);
    
      A* printBegin = new B();
      A* printAll = new C();
      //faking virtualism will print just begin
      printBegin->Print(numbers.begin(), numbers.end());
      //faking virtualism will print all
      printAll->Print(numbers.begin(), numbers.end());
    }
    

    OUTPUT:

    Begin::0
    Iterator::0
    Iterator::1
    Iterator::2
    Iterator::3
    Iterator::4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize that the method run() must be declared because its declared in the
I realize that tinyint is a single byte integer (by the way, is it
I realize that a SO user has formerly asked this question but it was
I realize that TWTweetComposeViewController is new to iOS 5 (which is now a bit
I realize that keywords and descriptions are old-school SEO techniques and many search engines
I realize that the answer to this question is likely quite obvious (if somewhat
I realize that this question has been asked 100times but none that I have
I realize that it can depend on certain things (and obviously how efficient the
(I realize that my title is poor. If after reading the question you have
I have a problem that I have not yet tested/compiled and wondering if it

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.