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

The Archive Base Latest Questions

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

The D Programming Language Version 2 has a nifty method to overload an expression

  • 0

The D Programming Language Version 2 has a nifty method to overload an expression like this:

classInstance[someName] = someValue;

Or as D Function defined in this little example:

ref Map opIndexAssign(ref const(ValueT) value, ref const(NameT) name)
{
    this.insert(name, value);
    return this;
}

Is this possible in C++ (ideally without using the STL)? If so, how?

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

    Normally, you would use a proxy object as the return type of operator[]; that object will have a custom operator= defined. The vector<bool> specialization in the C++ Standard Library uses a proxy to get the behavior you are looking for. The proxy-based solution isn’t as transparent as the D version, though. The code is something like:

    class proxy;
    
    class my_map {
      public:
      proxy operator[](const key_type& k);
      // Rest of class
    };
    
    class proxy {
      my_map& m;
      key_type k;
      friend class my_map;
      proxy(my_map& m, const key_type& k): m(m), k(k) {}
    
      public:
      operator value_type() const {return m.read(k);}
      proxy& operator=(const value_type& v) {m.write(k, v); return *this;}
    };
    
    proxy my_map::operator[](const key_type& k) {
      return proxy(*this, k);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which programming language(s) were used to code Windows Vista?
Where X is any programming language (C#, Javascript, Lisp, Perl, Ruby, Scheme, etc) which
In the C programming language and Pthreads as the threading library; do variables/structures that
C++ was the first programming language I really got into, but the majority of
Is there a programming language suitable for building web applications, that is compiled, strongly-typed,
In his The C++ Programming Language Stroustrup gives the following example for inc/dec overloading:
Can anyone suggest me a helpful programming language which can be used to create
Below are lines from the c++ programming language template<class T > T sqrt(T );
I'm reading The C++ Programming Language and in it Stroustrup states that the int
I'm reading The C++ Programming Language. In it Stroustrup states that sizeof(char) == 1

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.