Earlier quoted context omitted.
That’s a bit like saying you know C++ but not streams or templates, or C but not floating point operations. It’s probably worth learning STL. Anyway, the reason to use move instead of copy is for performance. Move constructors are faster because they can leave the source object modified (e.g., take over control of a pointer to deep contents). This falls apart when the move constructor can throw, because the container…
I phrased that badly - it should have been “I don’t know every edge case in the STL, and so I don’t know why this would have different behavior”. However thanks for explaining the issue. This one is obvious and I just completely failed to think about how you ensure the source object is in a safe state if an exception occurs part way through moving the source data. It seems to imply the old MSVC behaviour was incorrec…
Mysterious Moving Pointers
51–60 of 68 posts
Re: Mysterious Moving Pointers
#52Earlier quoted context omitted.
> I remember the C++ ca. 1994 year when I started my career. It was C with Objects back then It is possible, and perhaps even likely that the C++ you wrote in 1994 was indeed this "better C" and maybe even the C++ you read, the language Stroustrup wrote about in 1985 although "C with Objects" is much older still. The ISO document (C++ 98 aka ISO 14882:1998) was four years into your future in 1994, but the committee t…
I believe that not many developers really wanted C++ and craved Object Pascal instead. I don't like Pascal but I have to admit that Object Pascal is a succint and successful addition of OO to a manually memalloc language, whereas C++ is neither.
A programming language that at a time offered similar security and features, with much better portability.
Never was a big C fan, with Object Pascal and C++ on my toolbox.
Re: Mysterious Moving Pointers
#53Earlier quoted context omitted.
I remember the C++ ca. 1994 year when I started my career. It was C with Objects back then. And it was great! C++ was better C, it was easy for any C dev to convince him/her to jump to it. I recently had to work with C++ code and... it is not a happy story anymore: - Lots of magic like described here - F**ing templates - for those who like them, did you ever see a C++ core file? Or tried to understand a single symbol…
> I remember the C++ ca. 1994 year when I started my career. It was C with Objects back then It is possible, and perhaps even likely that the C++ you wrote in 1994 was indeed this "better C" and maybe even the C++ you read, the language Stroustrup wrote about in 1985 although "C with Objects" is much older still. The ISO document (C++ 98 aka ISO 14882:1998) was four years into your future in 1994, but the committee t…
Borland C++ 2.0, or 3.0, rewrote BIDS from preprocessor magic into templates.
CSet++ for OS/2, also early 1990s also offered template based collections.
By 1996, MFC introduced template based collection classes as well.
Re: Mysterious Moving Pointers
#54There is a lesser known cousin to std::vector that doesn't have to move nor copy its elements when adding new elements, and that is std::deque.
Re: Mysterious Moving Pointers
#55Earlier quoted context omitted.
I remember the C++ ca. 1994 year when I started my career. It was C with Objects back then. And it was great! C++ was better C, it was easy for any C dev to convince him/her to jump to it. I recently had to work with C++ code and... it is not a happy story anymore: - Lots of magic like described here - F**ing templates - for those who like them, did you ever see a C++ core file? Or tried to understand a single symbol…
> - F*ing templates - for those who like them, did you ever see a C++ core file? Or tried to understand a single symbol? I have 20+ years of experience writing C++. Yes, I've looked at "core" C++ headers and source. The most annoying part to me is style (mixed tabs and spaces, curly braces at wrong places, parenthesis or not, the usual style complaints). But other than that they're very readable to a seasoned C++ eng…
> I have 20+ years of experience writing C++.
> Yes, I've looked at "core" C++ headers and source.
Re: Mysterious Moving Pointers
#56struct Node {
std::vector connections;
};struct Connection {
Node* from, to;
};Does this mean that to create the vector of connections, Nodes are created, and references are taken to store in the Connections? And then the Nodes are stored in the list, with std::move()?
I don't understand why you would want to go down that road. Intuitively, I would assume that you are not safe from an object copy somewhere down the line and your graph then comes crashing down like a house of cards. Wouldn't it make more sense to store the nodes as pointers? If you like to live dangerously, something like:
struct Graph {
std::vector> nodes;
};Or better:
struct Graph {
std::vector>> nodes;
};The later will give you plenty of warnings if you do not copy Nodes around with std::move().
Or less performant, but maybe safer, std::shared_ptr, together with:
struct Connection {
std::weak_ptr from, to;
};so that you have some check guarantees before access?
Re: Mysterious Moving Pointers
#57Re: Mysterious Moving Pointers
#58This is a great reminder of the pox that was Microsoft of the early part of the millennium. Besides an allergy to investing in web standards, they were woefully behind in their language support. Their non-adoption of modern C++ standards held client security back for a decade, and arguable held language standards development back.
Having only worked with gcc and clang, OP’s code looked completely fine to me and I was baffled why many comments think the code is at fault. Judging by this page [1], I agree this is entirely MSVC’s doing. [1] http://howardhinnant.github.io/container_summary.html
In reality, it's a defect in the standard, plus a quality-of-implementation issue in the Microsoft's C++ Standard Library.
Re: Mysterious Moving Pointers
#59I know C++ the language but not the STL (the overwhelming abundance of UB and total lack of safety make it an anathema), so my question is why the STL allows/requires non-move here copying here dependent on whether an object has a no throw move constructor? Note I’m not asking about move constructor vs memmove/cpy but rather the use of copy constructor vs move depending on exception behavior? Is it something like pre…
UB is a feature; people who keep on fighting it are such a pain. Regarding your question, nothrow operations are essential to maintaining invariants. And maintaining invariants is how you make code correct in a world where UB exists.
The problem with this flavour of UB being a programmer's promise "this is fine, trust me, no run-time checks needed" to the compiler is that a) it's made by the programmer by omitting said run-time checks ― and that often happens accidentally, not intentionally; b) the compilers are really bad at pointing out to the programmer places where they took advantage of such promises, which really complicates the task of writing conforming programs. Every time I add two int's, I promise to the compiler that an overflow won't happen: and of course, the moment an UB happens, all invariants cease to hold, so trying to find the initial bug where you've accidentally broke one of invariants turns into a nightmare.
Re: Mysterious Moving Pointers
#60I must be misunderstanding something from this article. With: struct Node { std::vector connections; }; struct Connection { Node* from, to; }; Does this mean that to create the vector of connections, Nodes are created, and references are taken to store in the Connections? And then the Nodes are stored in the list, with std::move()? I don't understand why you would want to go down that road. Intuitively, I would assum…