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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:04:31+00:00 2026-05-25T18:04:31+00:00

I want to do: template <class Derived=BattleData> class BattleData : public BattleCommandManager<Derived> { };

  • 0

I want to do:

template <class Derived=BattleData>
class BattleData : public BattleCommandManager<Derived> {
};

But obviously BattleData isn’t declared, so I tried a forward declaration:

template <class T> class BattleData;

template <class Derived=BattleData>
class BattleData : public BattleCommandManager<Derived> {
};

But then I get

error: “wrong number of template parameter on the second line, with
BattleData.

I really fail to see a solution to this!

Edit:

The reason I’m doing this is because I want to be able to use BattleData directly as a class, but I also want to be able to subclass it in which case I have to specify the derived class as the second template parameter.

For example let’s say the corpus of my BattleData class is :

template <class Derived> class BattleData: public BaseClass<Derived> {
    void foo1(){};
    void foo2(){};
    void foo3(){};
}

And I have a subclass

template class SubBattleData: public BattleData<SubBattleData> {
    void foo1(){};
}

I would still want, in some cases, to be able to write code like this:

BattleData *x = new BattleData(...);

I can’t even do the following without being able to use default arguments:

BattleData<BattleData> *x = new BattleData<BattleData>(...);

On one side, the reason functions aren’t virtualized in the BattleData class is the benefit of having no virtual function. The other reason it doesn’t work for me is that one of the parent CRTP classes invokes functions only if they’re present in the derived type (using decltype(Derived::function) and enable-if like structures), and fall back to default behavior otherwise. Since there can be a great deal of those functions with a particular design pattern (like a CRTP that reads a protocol with many different cases and processes a case a particular way only if the derived class specify the corresponding function, otherwise just transfer it without processing).

So those functions can be present in SubBattleData and not BattleData, but both classes would work fine if instantiated, yet it’s impossible to instantiate BattleData.

  • 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-25T18:04:31+00:00Added an answer on May 25, 2026 at 6:04 pm

    Here is how I solved it:

    template <class Derived> class BattleDataInh: public BaseClass<Derived> {
        void foo1(){};
        void foo2(){};
        void foo3(){};
    };
    
    template class SubBattleData: public BattleDataInh<SubBattleData> {
        void foo1(){};
    };
    
    class BattleData : public BattleDataInh<BattleData> {
    };
    

    And that way, I can add any other template parameters too. The solution was in front of my eyes the whole time but I didn’t see it…

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

Sidebar

Related Questions

I want to create a template class in C#, for example: public class Foo<T>
#include <iostream> template <class Derived> class Base { public: void method1() { static_cast<Derived*>(this)->method1(); }
I want to create a template class (let's call it Foo ) that only
I want to have a template class that looks something like what I have
I want to instantiate 2 property pages from the same class and template, because
I want to do something like this: template<template<int d, class> class container, int dim
I have a template class MyTemplate. It works fine. But as soon as I
I want to be able to define template <class TX> void f(const TX &x){
I have a class derived from slider which uses a custom control template and
If I want to use a member of a template base class from a

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.