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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:20:22+00:00 2026-05-16T00:20:22+00:00

I have just read an article about the Curiously Recurring Template Pattern. And you

  • 0

I have just read an article about the Curiously Recurring Template Pattern. And you can use it simulate virtual functions with templates.

For example:

template<class T>
struct A
{
    void func() { static_cast<T*>(this)->func(); }
};

struct B : public A<B>
{
    void func() { cout << "B" << endl; }
};`

However, if we have many subclasses from A and want to put them all in vector, for example, vector<A*> this is not possible when you use templates, and you have to use normal polymorphism with virtual functions in the base class.

What would be a design suggestion to solve this? Because I want to use templates, but also be able to use all subclasses together in a container.

  • 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-16T00:20:23+00:00Added an answer on May 16, 2026 at 12:20 am

    You could do this:

    class NonTemplateBase
    {
    };
    
    template<class T>
    struct A : public NonTemplateBase
    {
        void func() { static_cast<T*>(this)->func(); }
    };
    

    and then create a vector of pointers to NonTemplateBase (i.e. std::vector<NonTemplateBase *>).

    However, I’m guessing you want to be able to call func() or other member functions on the objects in your vector — and you want those calls to polymorphically call the correct member function in the derived class.

    This isn’t going to work because the Curiously Recurring Template Pattern only allows you to do static (i.e. compile-time) polymorphism. If you want runtime polymorphism, you have to use virtual functions.

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

Sidebar

Related Questions

I have just read this interesting article about the implementation details for various languages
I have just switched from svn to mercurial and have read some tutorials about
I recall I have read about a parser which you just have to feed
I have read much information about agile and waterfall and I just cannot think
I have read a huge pile of problems and solutions, and I just can't
I just read a fantastic article about WCF transactions and flow. Pretty much only
I just read this article: The Truth About Garbage Collection In section A.3.3 Invisible
I just read this article about the Entity Framework 4 (actually version 2). Entity
I've just read an interesting article about how Microsoft seems to be moving towards
I have a question about remote threads.I've read Mike Stall's article present here: <

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.