Day 1 Keynote - Bjarne Stroustrup: C++11 Style

By: chr1skol1

655   10   71660

Uploaded on 08/26/2012

Bjarne Stroustrup

Comments (4):

By anonymous    2017-09-20

Default data structure to think of in C++ is the Vector.

Consider the following points...

1] Traversal:
List nodes are scattered everywhere in memory and hence list traversal leads to cache misses. But traversal of vectors is smooth.

2] Insertion and Deletion:
Average 50% of elements must be shifted when you do that to a Vector but caches are very good at that! But, with lists, you need to traverse to the point of insertion/deletion... so again... cache misses! And surprisingly vectors win this case as well!

3] Storage:
When you use lists, there are 2 pointers per elements(forward & backward) so a List is much bigger than a Vector! Vectors need just a little more memory than the actual elements need.

Yout should have a reason not to use a vector.


Reference:
I learned this in a talk of The Lord Bjarne Stroustrup: https://youtu.be/0iWb_qi2-uI?t=2680

Original Thread

By anonymous    2017-09-20

This issue of getting compiler-enforced semantic types can crop up in all sorts of situations, from your situation to co-ordinate systems with different origins (where the values are all the same type (eg. int), but semantically, the types must not be mixed, because they represent offsets from different origins (x,y,z=0,0,0)--this occurs frequently in mathematics, where, when graphing the quadrant with positive x and y, the origin is in the lower left, and computer science where it is very common to place the origin upper left) to spaceship navigation (more on this last below).

In 2012, Bjarne Stroustrup gave an interesting talk on what he called type-rich programming introducing compiler-enforced semantic type safety with C++11 using templates, user-defined literals, a claimed no run-time overhead implementation and even tales of lessons learned from the Mars Climate Observer snafu ($350M spacecraft + mission lost because of lack of enforced semantic type safety). You can see the part of the talk where he covers semantic types here: https://youtu.be/0iWb_qi2-uI?t=19m6s

I've written up a sample code excerpt based on Stroustrup's demo code, updated to current standards and with the required operator overloads implemented). Unlike Bjarne's example, this one actually compiles. ;)

Gist of this code can be found here: https://gist.github.com/u-007d/361221df5f8c7f3466f0f09dc96fb1ba

//Compiled with clang -std=c++14 -Weverything -Wno-c++98-compat main.cpp -o main

#include <iostream>
#include <string>

template<int M, int K, int S>  //Meters, Kilograms, Seconds (MKS)
struct Unit
{
    enum { m=M, kg=K, s=S };
};

template<typename Unit> //a magnitude with a unit
struct Value
{
    double val; //the magnitude
    constexpr explicit Value(double d) : val(d) {} //construct a Value from a double
};

//Basic Semantic Units for MKS domain
using Meter = Unit<1, 0, 0>;
using Kilogram = Unit<0, 1, 0>;
using Second = Unit<0, 0, 1>;
using Second2 = Unit<0, 0, 2>;

//Semantic Value Types for MKS domain
using Time = Value<Second>;
using Distance = Value<Meter>;
using Mass = Value<Kilogram>;
using Speed = Value<Unit<1, 0, -1>>; //Speed is meters/second
using Acceleration = Value<Unit<1, 0, -2>>; //Acceleration is meters/second^2

//Operator overloads to properly calculate units (incomplete; for demo purposes)
Speed operator/(const Distance& lhs, const Time& rhs)
{
    return Speed(lhs.val / rhs.val);
}

Acceleration operator/(const Speed& lhs, const Time& rhs)
{
    return Acceleration(lhs.val / rhs.val);
}

//Define literals
constexpr Distance operator"" _m(long double ld)
{
    return Distance(static_cast<double>(ld));
}

constexpr Mass operator"" _kg(long double ld)
{
    return Mass(static_cast<double>(ld));
}

constexpr Time operator"" _s(long double ld)
{
    return Time(static_cast<double>(ld));
}

constexpr Acceleration operator"" _s2(long double ld)
{
    return Acceleration(static_cast<double>(ld));
}

int main()
{
    Speed sp = Distance(100)/Time(9.58); //Not bad, but units could be more convenient...
    Distance d1 = 100.0_m; //A good distance to run a race
    Speed sp1 = 100.0_m/9.58_s; //A human can run this fast
//    Speed sp2 = 100.0_m/9.8_s2; //Error: speed is m/s, not m/s^2
//    Speed sp3 = 100.0/9.8_s; //Error: 100 has no unit
    Acceleration ac1 = sp1/0.5_s; //Faster than any human

    return EXIT_SUCCESS;
}

Original Thread

Recommended Books

    Popular Videos 723

    Submit Your Video

    If you have some great dev videos to share, please fill out this form.