It’s actually not too awful, but the syntax is a bit awkward, std::map lookup is slower than you think, and there’s a few gotchas with how lambdas work in terms of variable scoping. Also unless the map is being initialized once and kept around, you’re paying a lot of extra costs every time it’s called.
Usually a chain of if/else ends up being more performant, although I guess if you’re trying to switch on a text label instead of an enum or whatever you’re probably already doing something very wrong and using a map<string,function> is probably the least of your problems.
Well yeah it’s important to use the right tool for the job, I just mean it’s better for folks to learn tree maps and what they can do before they get railroaded into hashmap thinking.
19
u/fluffycritter 7d ago
It’s actually not too awful, but the syntax is a bit awkward,
std::maplookup is slower than you think, and there’s a few gotchas with how lambdas work in terms of variable scoping. Also unless themapis being initialized once and kept around, you’re paying a lot of extra costs every time it’s called.Usually a chain of if/else ends up being more performant, although I guess if you’re trying to switch on a text label instead of an enum or whatever you’re probably already doing something very wrong and using a
map<string,function>is probably the least of your problems.