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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:22:46+00:00 2026-06-17T14:22:46+00:00

Consider the following situation in C++: template<int n> class Base { … }; class

  • 0

Consider the following situation in C++:

template<int n>
class Base { ... };

class Derived3 : public Base<3> {
  // a complicated body, making use of n=3
};

class Derived7 : public Base<7> {
  // a completely different body, making use of n=7
};

Inside of the Derived3 member functions, I would like to explicitly use n=3, and inside Derived7, n=7, without hardcoding the numbers, i.e., still referring to something like a template argument n. The following options come to my mind:

  1. Also templating the derived classes on n, and then using typedef. This way, the derived classes know n:

    template<int n>
    class DerivedTemplate3 : public Base<n> { ... };
    typedef DerivedTemplate3<3> Derived3;
    
    template<int n>
    class DerivedTemplate7 : public Base<n> { ... };
    typedef DerivedTemplate7<7> Derived7;
    

    The problem with this is that DerivedTemplateX makes sense for nothing but n=X, so this feels like abusing the template paradigm.

  2. Using a static const member to store n in Base, and referring to that in the derived classes:

    template<int n>
    class Base {
    protected:
      static const int nn = n;
      ...
    };
    
    class Derived3 : public Base<3> {
      // refer to nn=3
    };
    
    class Derived7 : public Base<7> {
      // refer to nn=7
    };
    

    The problem here is that I seemingly can’t use the same identifier (nn vs. n). Also, I’m not sure whether this will allow me to use nn as a template argument for members of the derived classes.

So: how can this be implemented in a non-redundant, efficient way? Maybe using some kind of static const int as a member somewhere?

  • 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-17T14:22:47+00:00Added an answer on June 17, 2026 at 2:22 pm

    The standard practice is to use an uppercase letter for the template parameter, then a static const value in lowercase:

    template<int N>
    class Base {
    protected:
      static const int n = N;
      ...
    };
    

    Then you use the lowercase static const value n everywhere – don’t use N anywhere else.

    Also, I’m not sure whether this will allow me to use nn as a template argument for members of the derived classes.

    It is a constant expression and so it can be used as a template argument.

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

Sidebar

Related Questions

Consider the following situation: class MyFoo { public: MyFoo(); ~MyFoo(); void doSomething(void); private: unsigned
Consider the following code: class B { int x; public: B() : x( 10
Consider the following situation: public class Storage { public T GetSingleByID<T>(long id) where T
Consider following 2 programs giving same error First calss: public class Testing { Testing
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]
Consider the following situation class URISplit { var $REQ_URI; //some more variables function __construct($uri)
To illustrate my question consider the following example: @Entity public class Box implements Serializable
So, please consider the following situation I have a super class of type Shapes
Consider the following situation: class A() { void a(); } @MyAnnotation class B extends
Consider the following situation: [Export] class A { } class B { [Import] private

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.