Notes for C++

C++


1. Like in Python, default constructors of classes take the arguments in the order or the fields:

// API: struct ColorU { r: u8, g: u8, b: u8 a: u8 } // therefore the arguments to the default constructor are passed in order: auto color = ColorU(/*r*/ 255, /*g*/ 255, /*b*/ 255, /*a*/ 255);

2. Explicit constructors are static functions, enums use enum class (C++11):

auto window = WindowCreateOptions::default(LayoutSolver::Default);

3. All by-value arguments require std::move in order to prevent accidental copies:

auto window = WindowCreateOptions::default(LayoutSolver::Default); app.run(std::move(window));

4. In difference to C, constructing a RefAny does not require macros, instead generics are used:

class MyStruct { int field; public: MyStruct(int f) noexcept: field(f) { } } auto object = RefAny::new(std::move(MyStruct(5))); auto objectref = object.downcastRef<MyStruct>(); if (objectref): // objectref = std::optional<Ref<MyStruct>> std::cout << objectref->ptr.field << std::endl; objectref.delete(); // release reference auto objectrefmut = object.downcastRefMut<MyStruct>(); if (objectrefmut):// objectrefmut = std::optional<RefMut<MyStruct>> std::cout << objectrefmut->ptr.field << std::endl; objectrefmut.delete(); // release reference // "object" RefAny destructor automatically decreases refcount here

5. All *Vec types have a _fromStdVector() function that converts a std::vector into the given Vec type without copying the contents of the array. Additionally a _fromStdString function exists to convert a std::string into a String type. All strings are UTF-8 encoded.

auto scancodes = std::vector{0,1,2,3}; auto converted = ScanCodeVec::fromStdVector(std::move(scancodes)); auto converted_back = converted.intoStdVector();

6. Like in C, all union enums have special functions to emulate pattern matching:

// create a union enum - Exact() is a constexpr function auto cursor = StyleCursorValue::Exact(StyleCursor::Grab); // destructure a union enum if (auto result = cursor.matchRefExact()) { // result = const StyleCursor* std::cout << result << std::endl; } if (auto result = cursor.matchRefMutExact()) { // result = StyleCursor* restrict *result = StyleCursor::Default; }