1

Please see the code below. I have use there const template types. The first line compiles, the other two don't. Why those two don't compile? And the first one which compiles - is it OK to write it? What is the difference of std::map<const int, const bool> and std::map<int, bool>?

std::map<const int, const bool> mm;
std::map<const int&, const bool> mm;
std::map<const int, const bool&> mm;

I know that this is a strange question but please help to clarify it.

Narek
  • 38,779
  • 79
  • 233
  • 389
  • I can understand it for the value, but for the key - no! See you write `myMap[key] = value;` therefore the value can't be reference type. What about the key? – Narek Apr 17 '13 at 12:57
  • You cannot use references in std::maps, see http://stackoverflow.com/questions/4239253/c-is-it-possible-to-use-a-reference-as-the-value-in-a-map – Akobold Apr 17 '13 at 12:58
  • Why? What if it is not? Why I get compile error? – Narek Apr 17 '13 at 13:03
  • Your answer to the third question in that comment is a direct result of the effects from the second, and the two in tandem, answer the first. – WhozCraig Apr 17 '13 at 13:05
  • You need l-value types for the key too because it copies the keys you pass into the nodes of the tree and the type in these nodes is the same as the one you passed as a template parameter, and not an unconsted version of it. – CashCow Apr 17 '13 at 13:23
  • @WhozCraig: I don't think *lvalue-type* is the correct thing to say. The term *lvalue* refers to an **expression**, not a type. And a expression that uses a reference is an *lvalue* expression anyways. – David Rodríguez - dribeas Apr 17 '13 at 13:43
  • @DavidRodríguez-dribeas Eh, you're, of course, correct, David. It wouldn't be my first butchery of vernacular. The intention is/was, understandable by you, but then again, you also knew then answer to the actual question going in. It likely is not as easily parseable by someone without a clue to the actual answer. So i'll drop said-comments in lieu of avoiding further confusion. – WhozCraig Apr 17 '13 at 15:07

1 Answers1

2

Why const value? map::value_type is really std::pair<const Key, Value>. You cannot store reference in any containter.

One of requirements from standard.

T& operator[](const key_type& x);

Requires: key_type shall be CopyInsertable and mapped_type shall be DefaultInsertable into *this.

But, const reference is not CopyInsertable.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • Why, for example in std::vector you need only copy ctor no? And the associative containers need copy ctor and operator<(). – Narek Apr 17 '13 at 13:06
  • @Narek: Because a vector just needs to copy (or move) elements; while the (ordered) associative containers also need to compare them to determine the order. – Mike Seymour Apr 17 '13 at 13:23
  • 1
    You picked `operator[]` that has the strictest requirements. You can use a map without using `operator[]` and the need for `DefaultInsertable` would be gone... – David Rodríguez - dribeas Apr 17 '13 at 13:44