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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:26:08+00:00 2026-06-13T18:26:08+00:00

I have something wrong with the new C++ unordered_map: I would like to use

  • 0

I have something wrong with the new C++ unordered_map: I would like to use the operator[] with a const key, but I get rejected.

I cannot give the whole code, but I can simplify my problem like this:

#include <unordered_map>

class A {
    public:
        A();
};

class B {
    public:
        B();
};

int main(int argc, char **argv) {
    std::unordered_map<A &, B> myMap;
    A a;
    const A &ar = a;
    B b;
    myMap[ar] = b;
}

The output of the compiler is a bit long, but ends with:

/usr/include/c++/4.6/bits/hashtable_policy.h:537:5: note:   no known conversion for argument 1 from ‘const A’ to ‘A&’

I use a const A & because, in my code, some method give it to me as is. And, by the way, the key should be const. I have tried a std::unordered_map<const A &, B> myMap; instead, but it does not work either.

I use gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5), with the -std=c++0x flag.

Could you please tell me why this is forbidden? I must say I do not understand the reason.

Many thanks (and please excuse me if the question is stupid…).

  • 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-13T18:26:09+00:00Added an answer on June 13, 2026 at 6:26 pm

    The reason is that operator[] is specified as follows (note that the same holds for just std::map):

    Value& operator[](Key const& k);
    

    In your case, Key is A&, so this expands to:

    B& operator[](A& const& k);
    

    And since references-to-references are invalid and the top-level reference is dropped when created through typedefs or template parameters, you get just:

    B& operator[](A&);
    

    Which can’t handle an A const& argument.

    In general, I’d advise against using a mutable reference as the key, since mutable keys are a good source for errors.

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

Sidebar

Related Questions

I'm new in unit testing so may be I do something wrong. I have
Maybe I'm doing something wrong but I have a textarea where I've specified textAlign
I have an array but something goes wrong with it, here is my code
I have something like the code below: for(int i=0;i<10;i++){ button=new JButton(buttons[i]); button.addActionListener(new ActionListener(){ public
i need a thread safe map, i have something like this: (i'm very new
I must have done something wrong. I have a C# project in Visual Studio
I think I have got something simple wrong here. I am trying to pass
I have something like this: function showFunction () { // need position and place
I have something like this: <?xml version=1.0 encoding=utf-8?> <Data> <ConfigDatas> <ArrayOfConfigData xmlns:i=http://www.w3.org/2001/XMLSchema-instance xmlns=http://schemas.datacontract.org/2004/07/BTTest.Models> <ConfigData>
I have the following ActionLink Html.ActionLink(Home, Index, Home, New With {.class = tab}) But

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.