<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Dec 3, 2020 at 4:19 PM Paul C. Anagnostopoulos via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I have the following map:<br>
<br>
  std::map<std::string, std::vector<Record *>> ClassRecordsMap;<br>
<br>
I call a function that builds a new vector of <Record *> and returns it.<br>
<br>
  const auto Records = getAllDerivedDefinitions(makeArrayRef(ClassName));<br></blockquote><div><br></div><div>This would be one of the times where using top level const on local variables will make things less good. You can't move from Records if it's const like that. (generally I don't think it's practical to use top level const in C++ pervasively - so I'd suggest generally not doing it, because doing it only sometimes may make things harder to read ("why is /this/ particular local variable const when lots of others aren't?" the future reader will ask themselves))</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
What is the best way to add it to the map, being careful not to copy it? This is what I'm doing now:<br>
<br>
  ClassRecordsMap[ClassNameString] = std::move(Records);<br></blockquote><div><br>Yep, generally that'll do it.<br><br>If you want to avoid even a default construction of std::vector (it's cheap, there's unlikely to be any reason to avoid that) you could use emplace:<br><br>ClassRecordsMap.try_emplace(ClassNameString, std::move(Records));<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>