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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:59:21+00:00 2026-05-23T21:59:21+00:00

C++ references tells us for a std::map typedef pair<const Key, T> value_type; Is it

  • 0

C++ references tells us for a std::map

typedef pair<const Key, T> value_type;

Is it possible to force the Key Type not to be const ?
I need to do this in a template method like

template<class T> // T represent a map in general (std::map, boost::unordered_map or whatever..)
void foo(const T& m)
{
  typename T::value_type::first_type x;
  x=0; // Wrong because x is const ...
}
  • 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-23T21:59:21+00:00Added an answer on May 23, 2026 at 9:59 pm

    No, it’s not.

    This is because map performs its internal ordering based on key. If you could modify the key yourself, willy-nilly, all hell would break loose.

    You should use the provided API functions; where the use of one results in changing a Key value (actually I don’t think that any do), the appropriate internal re-ordering may take place.

    Think of getters and setters, and their use in providing an alternative to messy/dangerous direct member access.


    However, you could write this:

    template<class T>
    void foo(const T& m)
    {
       typename T::key_type x;
       x = 0;
    }
    

    std::map type aliases

    key_type                Key
    mapped_type             T
    value_type              pair<const Key,T>
    key_compare             Compare
    value_compare           Nested class to compare elements
    allocator_type          Allocator
    reference               Allocator::reference
    const_reference         Allocator::const_reference
    iterator                Bidirectional iterator
    const_iterator          Constant bidirectional iterator
    size_type               Unsigned integral type (usually same as size_t)
    difference_type         Signed integral type (usually same as ptrdiff_t)
    pointer                 Allocator::pointer
    const_pointer           Allocator::const_pointer
    reverse_iterator        reverse_iterator<iterator>
    const_reverse_iterator  reverse_iterator<const_iterator>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm declaring a map of string to a pair of pairs as follow: std::map<std::wstring,
I have a std::map that is used by multiple threads to store data. The
I have a class: class foo { private: std::string data; public: foo &append(const char*
Possible Duplicate: What are the differences between struct and class in C++ http://www.cplusplus.com/reference/std/typeinfo/type_info/ I
I cannot figure out why this is not working. I will put up all
What references offer a good summary/tutorial for using RDF/OWL? There seem to be enough
Most references I've seen, and my IDE's code completion all have my specifying a
I've seen references to curried functions in several articles and blogs but I can't
Are there any good references for synchronisation algorithms? I'm interested in algorithms that synchronize
I have seen the references to VistaDB over the years and with tools like

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.