2024: Domino Containers - The Next Step. News from the Domino Container commu...
Modern C++ for Beginners
1. Modern C++ for Beginners
Kangjun Heo
November 5, 2019
ARGOS, Chungnam National University
Kangjun Heo, Modern C++ for Beginners 1/44
2. Presenter
• Kangjun Heo
• 17’ Computer Science and Engineering
• Chugnam National University since 2019
• ARGOS since 2019 spring semester
• knowledge@o.cnu.ac.kr / GitHub @0x00000FF
Kangjun Heo, Modern C++ for Beginners 2/44
3. Target Audiences
I recommend this presentation for these people...
• I am going to take Object Oriented Design
• I know C, going to use C++
• I am using C++ but going to use modern C++
Then I strongly recommend to listen this session!
Kangjun Heo, Modern C++ for Beginners 3/44
4. Contents
We’re going to see about:
• New keywords(constexpr, nullptr, auto)
• R-value Reference, Move semantics
• Smart Pointers
• ...and Lamba Expressions!
Kangjun Heo, Modern C++ for Beginners 4/44
5. Disclaimer
These slides contain very Basic informations of Modern C++!
Modern C++ has a lot of features than you thought...
Kangjun Heo, Modern C++ for Beginners 5/44
7. Getting Started
For g++(gcc) users...
• g++ -std=c++11 example.cpp
For clang users...
• clang -std=c++11 exmaple.cpp
You can use c++14 or c++17 as an argument.
Kangjun Heo, Modern C++ for Beginners 7/44
8. Getting Started
For Visual Studio users...
• Properties - C/C++ - Language
Kangjun Heo, Modern C++ for Beginners 8/44
9. auto
C has a keyword auto
• It means ”local scoped variable”
• you can use it like this: auto a = 3;
• this is auto int equivalent
However...
• C++ 98 abandoned such syntax
• Also, C99 Abanoned it
• But Modern C++ picked it from the graveyard
Kangjun Heo, Modern C++ for Beginners 9/44
10. auto
In modern C++, auto is compile-time type deduction
auto a = 3; // a is int
auto fun() { // fun returns const char*
return "helloc";
}
Kangjun Heo, Modern C++ for Beginners 10/44
11. nullptr
Remember NULL? It’s a constant that is from C, equals 0...
• In strict, it’s not a null pointer but integer!
#define NULL 0
Kangjun Heo, Modern C++ for Beginners 11/44
13. nullptr
So, a new keyword nullptr and its type nullptr_t
appeared...
int* pIntVar = nullptr;
Kangjun Heo, Modern C++ for Beginners 13/44
14. nullptr
So, a new keyword nullptr and its type nullptr_t
appeared...
if ( pIntVar == nullptr )
Kangjun Heo, Modern C++ for Beginners 14/44
15. constexpr
In the beginning, C’s const exists...
const int a = 3; // constant?
int square(int num) {
int a = 3, b = 2;
const int ab = a + b;
return num * num;
}
Kangjun Heo, Modern C++ for Beginners 15/44
17. constexpr
• compile-time constant
• evaluated at compile-time
• functions, variables, objects...
Kangjun Heo, Modern C++ for Beginners 17/44
18. constexpr
int a = 3, b = 2;
constexpr int ab = a + b;
• err: the value of ’a’ is not usable in a constant expression
• only constexpr-able value or symbols are accepted
Kangjun Heo, Modern C++ for Beginners 18/44
19. constexpr
• Example: Factorial
constexpr int fac(int n)
{
return n > 0 ? n * fac(n-1) : 1;
}
int main()
{
int a = fac(4);
}
Kangjun Heo, Modern C++ for Beginners 19/44
21. L-value vs R-value
L-Value(lhs) = R-Value(rhs)
• L-Value: A expression that have explicit memory space
• R-Value: Temporary Data or Object(e.g. Literals)
• Actually, R-Value is a complementary set of L-Value
Kangjun Heo, Modern C++ for Beginners 21/44
22. R-Value Reference
int a = 3;
int& b = a; // ok
int& c = 3; // not ok
int&& d = 3; // ok
Kangjun Heo, Modern C++ for Beginners 22/44
23. Move Semantics
SomeType a = SomeRValue;
// Calls SomeType::operator=(SomeType&)
• R-Value cannot be referenced
• Create and copy into new object to provide L-Value
Kangjun Heo, Modern C++ for Beginners 23/44
24. Move Semantics
SomeType a = std::move(SomeRValue);
// Calls SomeType::operator=(SomeType&&)
Kangjun Heo, Modern C++ for Beginners 24/44
25. Pop Quiz
How many methods this class have in Modern C++?
class A { };
Hint In legacy C++, there were 4 methods in empty class.
Kangjun Heo, Modern C++ for Beginners 25/44
26. Pop Quiz
Answer: 6
class A {
public:
A();
A(const A&);
A& operator=(const A&);
~A();
A(A&&) noexcept;
A& operator=(A&&) noexcept;
};
Kangjun Heo, Modern C++ for Beginners 26/44
27. RAII & Ownership
Resource Acquisition Is Initialization
• Resource acquisition must be done when initialized
• Every valid objects must acquire necessary resources
• Object that required resources are not valid
Kangjun Heo, Modern C++ for Beginners 27/44
28. RAII & Ownership
Ownership
• So called Ownership Semantics
• Responsibility of the owner of a chunk of dynamically
allocated memory to release that memory.
Kangjun Heo, Modern C++ for Beginners 28/44
29. Smart Pointer
void fun() {
// acquired SomeType
const SomeType* somePtr = new SomeType;
// do something useful...
// somePtr is passed several subroutines
...
}
// fun() ended, but somePtr's not released
• Raw pointers are not ”Object”
• Address for SomeType Object can be shared with
unregulated manner
Kangjun Heo, Modern C++ for Beginners 29/44
30. Smart Pointer
Smart Pointers are declared in STL header <memory>
• std::unique_ptr<T>
• std::shared_ptr<T>
• std::weak_ptr<T>
Kangjun Heo, Modern C++ for Beginners 30/44
31. std::unique_ptr<T>
• Only one unique_ptr can own a resource.
• Resource is automatically released once unique_ptr is
destroyed.
auto uPtr =
new std::unique_ptr<SomeType>(
new SomeType()
); // C++ 11
auto uPtr =
std::make_unique<SomeType>(); // C++ 14
Kangjun Heo, Modern C++ for Beginners 31/44
32. std::shared_ptr<T>
• One or more shared_ptr can own a resource.
• It has ’Reference counter’, indicates how many
shared_ptrs are having onwership.
• Once Reference counter reaches 0, the resource will be
released.
auto sPtr =
new std::shared_ptr<SomeType>(
new SomeType()
); // C++ 11
auto sPtr =
std::make_shared<SomeType>(); // C++ 14
Kangjun Heo, Modern C++ for Beginners 32/44
33. std::weak_ptr<T>
• Has no ownership for the resources
• Don’t affect reference counter
• Provides some observation methods
Kangjun Heo, Modern C++ for Beginners 33/44
34. std::weak_ptr<T>
auto sp = std::make_shared<SType>();
std::weak_ptr<SType> wp = sp;
if (auto sp2 = wp.lock()) { // get from wp
// do with sp2 is not nullptr
}
if (!wp.expired()) { // check if deleted
// do something when sp is not expired
}
Kangjun Heo, Modern C++ for Beginners 34/44
35. Lambda Expressions
Lambda is a unnamed function object.
[capture] (args) -> returnType {
//body
}
Kangjun Heo, Modern C++ for Beginners 35/44
36. Lambda Expressions
Lambda Capture
• & means capture by-reference.
• = means capture by-copy. -> default
[] () { } // capture default (this)
[=] () { } // capture by-copy default (same)
[=, &r] () { } // copy all, but r is by-ref
[&] () { } // by-ref default
[&, c] ( ) { } // ref all, but c is by-copy
Kangjun Heo, Modern C++ for Beginners 36/44
37. Lambda Expressions
[] { std::cout << "hi"; }(); //just call
// c is std::vector
std::for_each(c.begin(), c.end(),
[](auto& item) {
// do for_each loop callbacks
}); // pass as argument
Kangjun Heo, Modern C++ for Beginners 37/44
38. Summary
From Modern C++, we got a bunch of useful features:
• auto, nullptr, constexpr
• R-Value Reference, Move Semantics
• Smart pointers
• Lambda Expressions
Kangjun Heo, Modern C++ for Beginners 38/44
39. Recommended Sites
For the references of C++ standard...
• cppreference (https://en.cppreference.com/)
And several C++ implmentations...
• GCC libstdc++ (https://github.com/gcc-mirror/gcc)
• Clang libcxx (https://github.com/llvm-mirror/libcxx)
• Microsoft STL (https://github.com/microsoft/STL)
Kangjun Heo, Modern C++ for Beginners 39/44