Tech Off Post

Single Post Permalink

View Thread: A moving question
  • User profile image

    > Why can't container::value_type invoke a move like make_pair can ?

    make_pair(k, v) returns pair<K, V>.

    map<K, V>::value_type is pair<const K, V>.

    Modifiable rvalues can be moved from, but const rvalues must be copied from.

    > Should it not use move instead?

    What it's doing is correct and required by the Standard. move() here would be incorrect (the reason is that the pair's element types might be references - it is VERY subtle).

    You should try to avoid looking at the Standard Library's implementation too much. It's full of complicated stuff (that we almost never explain with comments; we'd like to someday, but for now we're way too busy), and doesn't always demonstrate proper style. For example, in other places we say "forward" when it's identical to "move" but we really should be saying "move". (One of the items on my todo list is to clean that up.)

    The rules for users to follow are simple: when moving, say move(). When perfect forwarding, say forward<T>(t). Never confuse the two.