4

First of all: I'm still a beginner in c ++, but I don't really understand the following:

So someone said to me, that if i have for example a container of references (e.g. map), it is not working because "under the hood, containers allocate memory and then assign into those spots. And references cant be assigned after they are created". Which made sense to me, but then i tested it with "const int" for example, and it did work, but why did it work? Doesnt const int have the same problem? Isnt it like the same, that i cant assign to "const" afterwards?

EDIT 2.0: So i basically mean this:

map<int, const int> m1{ {20,20} }; //Working, but why?
map<int, int&&> m2{ {20,20} }; //Not working, makes sense.

Sorry for my english, I hope you could understand what I meant.

Avva
  • 150
  • 6
  • 2
    When I try to make a `std::vector`, it [refuses to compile](https://godbolt.org/z/h9Gj6M84M), with an extremely verbose error message that I think boils down to the issue you're describing of them not being reassignable. What is your actual code and how are you compiling it? – Nathan Pierson Sep 18 '21 at 23:52
  • Sorry, i posted a Edited version in the original post with an example – Avva Sep 18 '21 at 23:57
  • 1
    @Avva try actually inserting an item into your `map` or `vector` of consts (e.g. `m1[5] = 6;`) – Jeremy Friesner Sep 18 '21 at 23:58
  • Thanks, i edited the original post again, sorry for the mistakes and inaccuracy! – Avva Sep 19 '21 at 00:02
  • 1
    Could it maybe be that if i put "const int" in map, it is allocating and initializing with the "new" keyword, like: new const int{..}; And if i put references in there, it cant do this, because you obviously cant allocate "references" because it is not an object? (Again sorry for my english, I try my best) – Avva Sep 19 '21 at 00:12
  • You aren't allowed to use neither `const` elements nor references for standard container elements. The C++ standard prohibits it, and the point of failure is up to each implementation. There's no point reasoning about it. https://stackoverflow.com/a/6955332/817643 – StoryTeller - Unslander Monica Sep 19 '21 at 01:26
  • @StoryTeller-UnslanderMonica That looks like a duplicate to me. – cigien Sep 19 '21 at 02:21
  • @cigien - My track record with duplicate closure recently is summarised by "wasted effort". Doesn't matter how hard I try to find one that fits, there's too many folks with a Mjolnir to reach a consensus. At least by commenting, the link may stay. – StoryTeller - Unslander Monica Sep 19 '21 at 02:24
  • @StoryTeller-UnslanderMonica I think I know which question you're referring to; I'm in that chatroom right now. Don't worry about a bit of wasted effort. That's bound to happen now and then. – cigien Sep 19 '21 at 02:26
  • @cigien - If it was only one question. I think I'm gonna lay back on this sort of curation. Can't afford the investment. – StoryTeller - Unslander Monica Sep 19 '21 at 02:28
  • @StoryTeller-UnslanderMonica I'm sorry to hear that. Of course, you shouldn't invest any time you can't afford. I do hope you find the time eventually, and can participate in this kind of curation. There's a lot of mjolnirs, but not many willing to use it. – cigien Sep 19 '21 at 02:47

0 Answers0