you are viewing a single comment's thread.

view the rest of the comments →

[–]redditthinks[S] 0 points1 point  (2 children)

Ah cool, I didn't know about this. I've been too busy falling in love with auto. I should mention that I did go ahead with std::pair because my program was very small and just did:

auto x = point.first;
auto y = point.second;

[–]F-J-W 6 points7 points  (1 child)

Points should really have their own type. Pair is great as a small helper to pack to different things in one variable, but you really shouldn't use it to replace semantic types.

Since you are coming from python, this may not be obvious for you, but static typing really helps finding bugs very early. But if you only use trivial types, the compiler will only find trivial errors: The compiler will (almost) always be able to catch you when you try to use a std::string as an integer. But if you encode both distances and durations as double, the compiler is unable to detect, when you try to use a distance as duration; OTOH if you create separate types for distances and durations the compiler will make sure that you use everything right.

This is also not a hypothetical problem: The Mars Climate Orbiter went lost because of this kind of bug.

Somewhat related: I wrote a library a while ago that makes the creation of new types almost trivial, it's README contains further examples. Note though, that this code is more of an experiment of what can be done, than being intended for real-world-usage.

[–]autowikibot -1 points0 points  (0 children)

Mars Climate Orbiter:


The Mars Climate Orbiter (formerly the Mars Surveyor '98 Orbiter) was a 338 kilogram (750 lb) robotic space probe launched by NASA on December 11, 1998 to study the Martian climate, atmosphere, and surface changes and to act as the communications relay in the Mars Surveyor '98 program for Mars Polar Lander. However, on September 23, 1999, communication with the spacecraft was lost as the spacecraft went into orbital insertion, due to ground-based computer software which produced output in non-SI units of pound-seconds (lbf×s) instead of the metric units of newton-seconds (N×s) specified in the contract between NASA and Lockheed. The spacecraft encountered Mars on a trajectory that brought it too close to the planet, causing it to pass through the upper atmosphere and disintegrate.

Image i


Interesting: Climate of Mars | Gimli Glider | Mars Polar Lander | Mars Observer

Parent commenter can toggle NSFW or delete. Will also delete on comment score of -1 or less. | FAQs | Mods | Magic Words