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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:53:40+00:00 2026-06-04T18:53:40+00:00

Possible Duplicate: Why isn't the [] operator const for STL maps? I’ve boiled this

  • 0

Possible Duplicate:
Why isn't the [] operator const for STL maps?

I’ve boiled this down to (what I think is) its simplest form:

#include <iostream>
#include <map>
#include <vector>

class NumberHolder
{   
public:
    NumberHolder( const std::string aKey, const double aNumber );
    void printSmallestMappedNumber( const unsigned int theIndex ) const;
private:
    std::vector< std::map< std::string, double> > theNamedNumberMapVector;
};

NumberHolder::NumberHolder( const std::string key, const double aNumber )
{
    std::map<std::string, double> aTempMap;
    aTempMap[key] = aNumber;
    theNamedNumberMapVector.push_back(aTempMap);
}

void NumberHolder::printSmallestMappedNumber( const unsigned int theIndex ) const
{
    std::map<std::string, double>& aSpecificMap = theNamedNumberMapVector[theIndex];
    std::cout << aSpecificMap["min"] << std::endl;
}

int main(int argc, char* argv[])
{
    NumberHolder aTaggedNumberHolder("min", 13);
    aTaggedNumberHolder.printSmallestMappedNumber( 0 );
    return 0;
}

I have a vector full of maps, and each map is full of (string) “tagged” numbers. I’m trying to keep as tight of control over access to the visibility/mutability of the variables involved.

Anyhow, the compiler fails with this error

Binding of reference to type ‘std::map’ to a value of type ‘const std::map, double, std::less >, std::allocator, double> > >’ drops qualifiers

My first (underbaked) attempt was making the map const…, as I’m not modifying the map, only retrieving a value from it:

const std::map<std::string, double>& aSpecificMap = theNamedNumberMapVector[theIndex];

Which then presents me with this admittedly shorter, but actually a bit more confusering error:

No viable overloaded operator[] for type 'const std::map<std::string, double>'

On the following line instead:

 std::cout << aSpecificMap["min"] << std::endl;

However, perhaps because I’ve been trying to untangle this for a bit, my solution seems to be awfully, awfully crufty:

std::map<std::string, double>& aSpecificMap = const_cast<std::map<std::string, double>&>(theNamedNumberMapVector[theIndex]);

const_casting away the [const derp] qualifier works around my problem, but I would really like to have a clear understanding of exactly what’s going on. I’m guessing that the compiler is disgruntled about my access to the map (in my second attempt, above) and believes that I will use & abuse my access to the map’s contents. I’d really like / need to be able to explain stuff like this to other people, as well as trying not to abuse the language and ultimately end up on The Daily WTF, because, you know, shame and stuff.

  • 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-04T18:53:42+00:00Added an answer on June 4, 2026 at 6:53 pm

    If you look at the declaration for map’s operator[], you will see that it is not const qualified, which is why it cannot be called on a const std::map<std::string, double>. The reason it is not const is because if the key does not exist in the map, it creates it. You should instead use find or at to get the element.

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

Sidebar

Related Questions

Possible Duplicate: Does readdir() guarantee an order? I'm guessing this isn't the case, and
Possible Duplicate: Why isn't the const qualifier working on pointer members on const objects?
Possible Duplicate: why isn't this pulling data from 7 days back? Here is what
Possible Duplicate: How do I use regular expressions in bash scripts? Why isn't this
Possible Duplicate: ASP.NET “special” tags I hope this isn't too much of a newbie
Possible Duplicate: Python conditional assignment operator Apologies for such a simple question, but googling
Possible Duplicate: In C++ why can’t I write a for() loop like this: for(
Possible Duplicate: Why there are no ++ and — operators in Python? This question
Possible Duplicate: Why isn’t sizeof for a struct equal to the sum of sizeof
Possible Duplicate: Why isn’t sizeof for a struct equal to the sum of sizeof

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.