C++ de-alienation / 祛魅
notes on c++ sanity checks; This is not about how to use c++ ; instead this is about what’s different and how does xyz work under the hood.
main reference:
- A nice tutorial/cheatsheet https://www.learncpp.com
- explains vtable and virtual functions https://pabloariasal.github.io/2017/06/10/understanding-virtual-tables/
- modern c features https://github.com/AnthonyCalandra/modern-c-features
- modern c++ features https://github.com/AnthonyCalandra/modern-cpp-features
Table of Contents
- string
- cast
- L/R-value
- move semantics
- class
- overloading
- virtual function and
vtable
- lambda
- attributes
- constexpr
- MISC
# string
https://www.learncpp.com/cpp-tutorial/introduction-to-stdstring/
std::string
- can have flexible length, involves malloc, slower
- passing
std::string
by value causes a copy, so don’t- use
std::string_view
orconst std::string&
as parameter
- use
- supports
move
semantics, returnstd::string
by value is fine - doesn’t work well with
constexpr
(usestring_view
) instead
|
|
string literal
|
|
std::string_view
(c++17)
- basically the rust slice
- provides read-only access to existing string (or another
string_view
) - no copy
string_view
parameter can take astd::string
(implicit conversion), no copy- do not return a
string_view
if created from a local string - not vice versa:
std::string_view
doesn’t implicitly convert tostd::string
- can create a
std::string
with astd::string_view
initializer orstatic_cast
astring_view
tostring
- fully supports
constexpr
- prefer to use
constexpr std::string_view
for string symbolic constants
|
|
string_view
lifetime
|
|
- do not initialize
std::string_view
fromstd::string
literal => danglingstring_view
# cast
static_cast
: can be used for any type cast that is allowed implicitly; or any type tovoid
, or any conversion that can be reversed.reinterpret_cast
: dangerous: casting a type directly to another, no guarantee.dynamic_cast
: for polymorphism. Cast a pointer to a class down/up the hierarchyconst_cast
: remove or addconst
to a variable(type) value
(c style cast)type(value)
(function-style cast)
# L/R-value
SRC: https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Lvalues.html SRC: https://en.cppreference.com/w/cpp/language/value_category
l/r-values in CLang are not necessarily on the “left/right side of assignment”, the naming is for historical reasons (CPL language).
# in C
L-Values an expression that identifies a memory space that holds a value.
- a variable
- a pointer dereference expression
using *
- structure field reference using
.
or->
- array-element reference using
[...]
, if the array is an lvalue - etc.
# in C++
each expression belongs to exactly one of the three value categories:
- pvalue: (“pure” rvalue)
- xvalue: (“eXpiring” value)
- lvalue: a gavlue that is not an xvalue
- (gvalue): (“generalized” lvalue), an expression whose evaluation determines the identity of an object or function
# move semantics
return an object by value without making a copy. //// TODO ////
# class
# overloading
#
virtual function and vtable
- https://www.geeksforgeeks.org/virtual-function-cpp/
- https://pabloariasal.github.io/2017/06/10/understanding-virtual-tables/
- Virtual Function
- a member function that is declared within a base class and is re-defined (overridden) by a derived class. (And a vtable will be created for the class); virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for that function call.
|
|
# lambda
https://github.com/AnthonyCalandra/modern-cpp-features/blob/master/CPP11.md#lambda-expressions
|
|
capture-list:
[]
capture nothing[=]
capture local objects by value (marked asconst
so immutable).[&]
capture local objects by reference[this]
capturethis
by reference[a, &b]
capturea
by value,b
by reference
# attributes
|
|
# constexpr
expressions that are “possibly” evaluated by compiler at compile time.
(“possible” .. you mean it’s not a guarantee?)
# MISC
- declare base class desctructor as virtual
decltype
- Temporary materialization
- rvalue reference (C++11):
T&&
, I’m not sure why it’s needed.. maybe to break a loooong nasty rvalue expression into smaller, logical units?
# nullptr
https://github.com/AnthonyCalandra/modern-cpp-features/blob/master/CPP11.md#nullptr
nullptr
can not be converted into inregral types except bool
# user defined literals
https://github.com/AnthonyCalandra/modern-cpp-features/blob/master/CPP11.md#nullptr