[llvm-dev] Question about std::map
Paul C. Anagnostopoulos via llvm-dev
llvm-dev at lists.llvm.org
Thu Dec 3 16:49:51 PST 2020
This just got weird. The 'const' was left over from previous experiments, so I removed it. Now when I run the -gen-instr-info backend on the AMDGPU target, I get:
error: ERROR: No 'Target' subclasses defined!
This suggests that there is a fundamental flaw somewhere. That message would be explained if the records pointed to by the vector of <Record *> were disappearing after the vector is stored in the map.
At 12/3/2020 07:24 PM, David Blaikie wrote:
>On Thu, Dec 3, 2020 at 4:19 PM Paul C. Anagnostopoulos via llvm-dev <<mailto:llvm-dev at lists.llvm.org>llvm-dev at lists.llvm.org> wrote:
>I have the following map:
>
>Â std::map<std::string, std::vector<Record *>> ClassRecordsMap;
>
>I call a function that builds a new vector of <Record *> and returns it.
>
>Â const auto Records = getAllDerivedDefinitions(makeArrayRef(ClassName));
>
>
>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))
>Â
>
>What is the best way to add it to the map, being careful not to copy it? This is what I'm doing now:
>
>Â ClassRecordsMap[ClassNameString] = std::move(Records);
>
>
>Yep, generally that'll do it.
>
>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:
>
>ClassRecordsMap.try_emplace(ClassNameString, std::move(Records));
More information about the llvm-dev
mailing list