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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:12:55+00:00 2026-05-22T23:12:55+00:00

I am creating a templated class D<N> , with a method (operator(), in this

  • 0

I am creating a templated class D<N>, with a method (operator(), in this case) that returns different types, depending on the value of N.

I could only make this work by creating two separate class declarations, but this came at the cost of a lot of code duplication. I also tried to create a common base class to throw the common stuff into, but I couldn’t get the constructor to inherit right and don’t know how idiomatic that would be as well…

#include <cstdio>

template <int N>
struct D{
    int s;
    D(int x):s(x){}

    int yell(int x){
        printf("N=%d, %d\n", N, s+x);
        return s+x;
    }

    D<N-1> operator()(int x){
        D<N-1> d(yell(x));
        return d;
    }
};

template <>
struct D<1>{
    int s;
    D(int x): s(x){}

    int yell(int x){
        printf("N=%d, %d\n", 1, s+x);
        return s+x;
    }

    int operator()(int x){
        return yell(x);
    }
};


int main()
{
    D<2> f(42);
    printf("%d\n", f(1)(2));
    return 0;
}

How can I make my code better looking?

  • 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-22T23:12:56+00:00Added an answer on May 22, 2026 at 11:12 pm

    You can use the Curiously Recurring Template Pattern.

    template<int N, template<int> typename D> struct d_inner {
        D<N-1> operator()(int x) {
            return D<N-1>(static_cast<D<N>*>(this)->yell(x));
        }
    };
    template<template<int> typename D> struct d_inner<1, D> {
        int operator()(int x) {
            return static_cast<D<1>*>(this)->yell(x);
        }
    };
    
    template <int N> struct D : public d_inner<N, D> {
        int s;
        D(int x):s(x){}
    
        int yell(int x){
            printf("N=%d, %d\n", N, s+x);
            return s+x;
        }
    };
    

    Not that I see the utility- or purpose- of this particular object being templated, it could easily not be.

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

Sidebar

Related Questions

I have a basic class that I extend fairly often. I thought it would
what's Difference b/w creating a RIA Services Solution (i.e. Enabling RIA Services checkbox) and
I would like to understand better the mechanics and the issues behind creating library
How can I (if possible) get to the ModelMetadata from a view without creating
I'm creating a Django project. I just tried taking the project out of debug,
How to add app item to main django admin site menu without creating any
Possible Duplicate: Can the template parameters of a constructor be explicitly specified? following up
How do I use django templates tags with jquery template tags ( link )
Using the Tab Bar template in Xcode 4, each ViewController is created automatically, so
I want to create a Visual Studio 2010 Project Template, which is essentially 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.