Skip to main content

Timeline for C++ Split string into a vector

Current License: CC BY-SA 3.0

11 events
when toggle format what by license comment
Apr 3, 2017 at 9:47 vote accept Simon Goodman
Apr 3, 2017 at 9:47 vote accept Simon Goodman
Apr 3, 2017 at 9:47
Apr 2, 2017 at 19:59 comment added Edward @JerryCoffin: That's a good point. I suppose that my experience with not-very-good compilers for obscure embedded processors somewhat colors my view on what a "modern" compiler does!
Apr 2, 2017 at 19:55 comment added Jerry Coffin @Edward: Note that a compiler doesn't have to be particularly modern to do RVO/NRVO. Just for example, VC++ 6 could do it, despite predating the original 1998 C++ standard.
Apr 2, 2017 at 17:33 history edited Edward CC BY-SA 3.0
fixed typo
Apr 2, 2017 at 16:46 comment added Edward Yes, as @Incomputable has already said, many modern C++ compilers do "return value optimization" (RVO) which allows them to skip the actual copy. All the compilers I use (clang, gcc, MSVC++) apply this optimization. You can test yours by looking at the generated assembly code.
Apr 2, 2017 at 16:45 comment added Simon Goodman Lol, thanks I just saw it... Thanks for that, it makes sense.
Apr 2, 2017 at 16:43 comment added Incomputable @SimonGoodman, I placed the comment above, since I knew you will ask that! RVO and NRVO are return value optimization and named return value optimization. Both of them elide a copy, e.g. construct the result right into the call site. Move constructor is also really cheap and fast.
Apr 2, 2017 at 16:43 comment added Simon Goodman Thanks for the response, isn't std::vector<std::string> Split(const std::string& subject) slower than void Split(const std::string& subject, std::vector<std::string> container) because of the copying that happens in the return?
Apr 2, 2017 at 16:39 comment added Incomputable Just to add: the resulting vector probably won't trigger copy constructor on return, but will probably be constructed in the destination. Also, return {...} would help on triggering RVO, while current code probably triggers NRVO. Even if neither of above happens, move constructor will kick in. Copying probably won't be slow, but depending on implementation it could decrease throughput.
Apr 2, 2017 at 16:30 history answered Edward CC BY-SA 3.0