Skip to content

Latest commit

 

History

History
56 lines (54 loc) · 1.75 KB

20220616204143-cpp_reference.org

File metadata and controls

56 lines (54 loc) · 1.75 KB

Cpp reference

Constants

const

constexpr

  • Optimization technique.
  • Use for compile time computation when function is called with constants
  • Function is called a runtime with invoked with non-constants

consteval

  • c++20
  • Same as constexpr but required to be evaluated at compile time.

Types

https://en.cppreference.com/w/cpp/language/type

Char

  • Use std::string over char lists (C strings). Safer to avoid buffer overflows.
  • You can turn a std::string (C++ string) into a C string with myCppString.c_str()

Auto

Have the compiler infer the type

typedef

Used to define a type

Arrays

Static

Dynamic

  • Use std:vector to declare a dynamic array and push_back() to add more elements and the array will be resized as needed.
    std::vector<int> someList(1); // init array with size of 1
    someList[0] = 1;
    someList.push_back(2); // Use push_back to add more items and resize array at runtime
        

Compiling

Basically each .cpp file can be compiled separately to create .o files which can be linked together to create one executable.

  • g++ -c file1.cpp: creates file1.o
  • g++ -c file2.cpp: creates file2.o
  • g++ my_executable file1.o file2.o: Links the files and creates the executable.

Makefile

Cmake

Using third party libraries

https://stackoverflow.com/a/10358977/5974855

Package managers

LSP

https://github.com/MaskRay/ccls

Spacemacs

https://www.youtube.com/watch?v=OjbkCEkboA8

Online Repl

https://replit.com/languages/cpp