Alex

Working with Types

Type Regularity

Semiregular Types

Equivalence and Equality

Initialisation and Assignment

Regular Types

Total Orderings

Destructors

Java vs C++ Semantics in Detail

C++ Classes

Singleton

template <typename T>
struct singleton
{
    T value;
}

Compiler Generated Functions

Semi-regular singleton

struct singleton {
    T value;
    
    singleton() {}
    ~singleton() {}
    
    // copy constructor
    singleton(singleton const& x)
        : value(x.value) { // this syntax is member initialisation syntax, which reduces unnecessary copying
          
    }
    
    // copy assignment
    singleton& operator=(singleton const& x) {
        value = x.value;
        return *this;
    }
}

Making Singleton Regular

friend bool operator==(singleton const& x, singleton const& y) {
    return x.value == y.value
}
friend bool operator!=(singleton const& x, singleton const& y) {
    return !(x==y)
}

Reasoning about Equality

Totally Ordered Singleton

Concepts (Parametric Polymorphism)

template <typename T>
    requires(std::regular<T> || std::semiregular<T> || std::totally_ordered<T>)
struct singleton final {
    //...
}

Instrumented Type

Summary