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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:01:05+00:00 2026-05-21T14:01:05+00:00

I have a container class called Properties . I want to add to it

  • 0

I have a container class called Properties. I want to add to it operator[](const std::string & name) which will return property with specified name.

Now lets consider that there is no property with specified name. In this case I wan’t to add new Property with specified name to my Properties if it is used as l-value and throw exception otherwise.

Properties pts;
pts.add("name1", val1);
pts.add("name2", val2);
pts["name1"] = val3; //OK
pts["name3"] = val1; //OK creating new Property with value = val1
cout << pts["name4"]; //Ooops can't find Property with name = "name4", so throwing an exception

Is this possible in C++? How can I write such operator[]?

  • 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-21T14:01:05+00:00Added an answer on May 21, 2026 at 2:01 pm

    You can cover the cases you give, but not by actually checking whether lvalue-to-rvalue conversion occurs. I don’t think it’s possible to directly intercept that, so you need to provoke a different conversion instead:

    • operator[] returns a proxy object, as John Zwinck says. Just creating this proxy object doesn’t create the key.

    • The proxy object has an operator=(const V&), so that handles assignment by creating the key. Optionally you could also have operator+=, operator++ and the rest – I’m not sure whether you mean that any lvalue use is OK, or just straight assignment.

    • The proxy object has a conversion to V& which throws if the key doesn’t already exist.

    Edit: this seems to vaguely work, although there are use-cases it doesn’t cover, such as passing the return value of operator[] to a function that takes V& and assigns to it in there. Also, hiding a proxy+conversion never results in a precisely equivalent interface to what you’d have with the original type, because implicit conversions can involve at most one user-defined conversion, and the proxy “uses up” that conversion.

    #include <iostream>
    #include <map>
    #include <string>
    #include <stdexcept>
    
    struct FunnyMap;
    
    struct ProxyValue {
        FunnyMap *ptr;
        std::string key;
        ProxyValue(const std::string &key, FunnyMap *ptr) : ptr(ptr), key(key) {}
        operator int&();
        int &operator=(int i);
    };
    
    struct FunnyMap {
        std::map<std::string, int> values;
        ProxyValue operator[](const std::string &key) {
            return ProxyValue(key, this);
        }
    };
    
    ProxyValue::operator int&() {
        if (ptr->values.count(key) != 0) {
            return ptr->values[key];
        } else {
            throw std::runtime_error("no key");
        }
    }
    
    int &ProxyValue::operator=(int i) {
        return ptr->values[key] = i;
    }
    
    void foo(int &i) {
        i = 4;
    }
    
    int main() {
        try {
            FunnyMap f;
            f["foo"] = 1;
            std::cout << f["foo"] << "\n";
            std::cout << f["bar"];
            // foo(f["bar"]); // also throws
        } catch (const std::exception &e) {
            std::cout << "Exception: " << e.what() << "\n";
        }
    }
    

    Output:

    1
    Exception: no key
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
In my program, I have a class called Object, which inherits from UIView. I
I have a dynamic class that serves as a storage container for configuration settings.
I have an solution in VS 2008 which contains two class library projects and
I have a class that contains two methods like these: public String getFoo(Int32 a)
I have a class that contains an array. I want this array to be
I have a custom container control (deriving from FlowLayoutPanel ) which contains zero or
I want to create an alias for a class name. The following syntax would
I have a class in VB.NET that has a method (called CurrentValue) that returns
I have a class that I need to binary serialize. The class contains one

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.