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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:44:22+00:00 2026-05-28T13:44:22+00:00

I have a class with operator() like this: struct S { int operator()(int a,

  • 0

I have a class with operator() like this:

struct S
{
    int operator()(int a, int b, int c, int d);
};

Example usage:

S s;
int i = s(1, 2, 3, 4);

I need my users to be able to use an alternate syntax:

int i = s[1][2][3][4]; // equivalent to calling s(1, 2, 3, 4)

I know I need to add S::operator[](int a) and that it needs to return a helper object. But beyond that it all gets a bit complex and I have a feeling that I am reinventing the wheel since other libraries (e.g. multidimensional arrays) probably already offer similar interface.

Ideally I’d just use an existing library to achieve this goal. Failing that, how can I achieve my goal with the most generic code?

Edit: ideally I’d like to achieve this without any runtime penalty on a modern optimizing compiler.

  • 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-28T13:44:23+00:00Added an answer on May 28, 2026 at 1:44 pm

    Here we go!

    First of all, the code is kind of messy- I have to accumulate the argument values as we go, and the only way I could think of (at least in C++03) is to pass the immediate indices set around as arrays.

    I have checked this on G++ 4.5.1 (Windows / MinGW) and I confirm that on -O3 the call:

    s[1][2][3][4];

    yields the same assembler code as:

    s(1,2,3,4);

    So – no runtime overhead if your compiler is smart with optimisations. Good job, GCC team!

    Here goes the code:

    #include <iostream>
    
    template<typename T, unsigned N, unsigned Count>
    struct PartialResult
    {
        static const int IndicesRemembered = Count-1-N;
        T& t;
        int args[IndicesRemembered];
        PartialResult(T& t, int arg, const int* rest) : t(t) {
            for (int i=0; i<IndicesRemembered-1; ++i) {
                args[i] = rest[i];
            }
            if (IndicesRemembered>0) args[IndicesRemembered-1] = arg;
        }
        PartialResult<T, N-1, Count> operator[](int k) {
            return PartialResult<T, N-1, Count>(t, k, args);
        }
    };
    
    template<typename T, unsigned Count>
    struct PartialResult<T, 0, Count>
    {
        static const int IndicesRemembered = Count-1;
        T& t;
        int args[IndicesRemembered];
        PartialResult(T& t, int arg, const int* rest) : t(t) {
            for (int i=0; i<IndicesRemembered-1; ++i) {
                args[i] = rest[i];
            }
            if (IndicesRemembered>0) args[IndicesRemembered-1] = arg;
        }
        void operator[](int k) {
            int args2[Count];
            for (int i=0; i<Count-1; ++i) {
                args2[i] = args[i];
            }
            args2[Count-1] = k;
            t(args2);
        }
    };
    
    template<typename T, unsigned Count>
    struct InitialPartialResult : public PartialResult<T, Count-2, Count> {
        InitialPartialResult(T& t, int arg)
            : PartialResult<T, Count-2, Count>(t, arg, 0) {}
    };
    
    struct C {
    
        void operator()(const int (&args)[4]) {
            return operator()(args[0], args[1], args[2], args[3]);
        }
        void operator()(int a, int b, int c, int d) {
           std::cout << a << " " << b << " " << c << " " << d << std::endl;
        }
        InitialPartialResult<C, 4> operator[](int m) {
            return InitialPartialResult<C, 4>(*this, m);
        }
    
    };
    

    And seriously, please, don’t use this and just stick with operator(). 🙂 Cheers!

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

Sidebar

Related Questions

I currently have a class like this: struct Rgb { static const int NUM_CHANNELS
I have this very simple wrapper template: template<class T> struct wrapper { inline operator
I want to have something like this below: template <class T> struct Container{ public:
I have code like this: class Base { public: void operator = (const Base&
I have a struct like this: struct A { void i(int i) {} void
Basically, I have a matrix class like this (with a lot of operator overloads
I have a class that defined a user defined operator for a TCHAR*, like
I would like to add an operator to a class. I currently have a
In C++, can you have a templated operator on a class? Like so: class
I have the following code: template <class T> struct pointer { operator pointer<const T>()

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.