static_casts/ reinterpret_cast TODO:

https://www.learncpp.com/cpp-tutorial/introduction-to-type-conversion-and-static_cast/

Literals

  #include <iostream>

int main(){
    std::cout << 5 << '\n';  // 5 (no suffix) is type int (by default)
    std::cout << 5L << '\n'; // 5L is type long
    std::cout << 5u << '\n'; // 5u is type unsigned int
    
    // basically the same as
    int a = 5;          // ok: types match
    unsigned int b = 6; // ok: compiler will convert int value 6 to unsigned int value 6
    long c = 7;         // ok: compiler will convert int value 7 to long value 7
}
  
Data typeSuffixMeaning
integralu or Uunsigned int
integrall or Llong
integralul, uL, Ul, UL, lu, lU, Lu, LUunsigned long
integralll or LLlong long
integralull, uLL, Ull, ULL, llu, llU, LLu, LLUunsigned long long
integralz or ZThe signed version of std::size_t (C++23)
integraluz, uZ, Uz, UZ, zu, zU, Zu, ZUstd::size_t (C++23)
floating pointf or Ffloat
floating pointl or Llong double
stringsstd::string
stringsvstd::string_view

Const

  • const
  • constexpr
  • #define

Enums

  enum season { 
	spring,
	summer,
	autumn,
	winter
};
  

compiler flags (#ifdef)

before compiling we can have some options for what code we want

For example, we can have code only for tests, simulations, or hardware

this is done through #ifdef

In Taproot the options are listed here

Namevariables
TestENV_UNIT_TESTS, PLATFORM_HOSTED, RUN_WITH_PROFILING
SimPLATFORM_HOSTED, RUN_WITH_PROFILING
HardwareN/A

We in Hardware mode so in the #ifdef block line 30 will not be included when compiling

Untitled.png

Here you see we are in Test mode so ENV_UNIT_TESTS is turned on. So line 30 will be included

Untitled.png

c++ practice

Using everything you learned try to do these:

  • simple ArrayList class (try adding these features one by one)
    • class field should have: size, capacity
    • should use a template and namespace
    • Default size #define size 4
    • Constructor should either take an list with values, or nothing and just create an empty array of default size.
    • methods:
      • constructor/deconstructor
      • get(int index)
      • edit(int index, T val)
      • double() doubles the array
      • append(T val)
      • print()
    • If you want more you can write sample classes for stacks, queues, trees, etc.