10. cyberplus
Integral User Defined Literals
Type operator "" _suffix(unsigned long long x);
Type operator "" _suffix(const char *str, size_t len);
Type operator "" _suffix<'c1','c2','c3'>();
11. cyberplus
Real User Defined Literals
Type operator "" _suffix(long double x);
Type operator "" _suffix(const char *str, size_t len);
Type operator "" _suffix<'c1','c2','c3'>();
12. cyberplus
String User Defined Literals
Type operator "" _suffix(const char *str, size_t len);
Type operator "" _suffix(const wchar_t *str, size_t len);
Type operator "" _suffix(const char16_t *str, size_t len);
Type operator "" _suffix(const char32_t *str, size_t len);
Type operator "" _suffix(char c);
Type operator "" _suffix(wchar_t c);
Type operator "" _suffix(char16_t c);
Type operator "" _suffix(char32_t c);
14. cyberplus
Attributes
Device dev0;
Device [[a1]] dev1 [[a2]];
Device dev2 [[a3, a4]];
Device dev3 [[a5(i,j)]];
Device dev4 [[NS::a6]];
[[a7]] while (!ready) { /* ... */ }
➢
Help convey information to compilers and tools
➢
Replacement for #pragma
➢
Usually present after named entities
➢
Usually present before unnamed entitites
19. cyberplus
constexpr data members
const
Runtime constants
const double PI=3.14;
const double PI_2=PI/2;
constexpr
Compile time constants
constexpr double PI=3.14;
constexpr double PI_2=PI/2;
Pre-processor Compiler Runtime
#define constexpr const
20. cyberplus
constexpr functions
Limitations in C++11:
• Non-void return type
• No variable or data type definition
• Can contain non-executable statements and static_asserts
• Single executable statement: return
• Arguments (if any) are compile-time constants
• All constexpr functions are also treated as const member functions if non-static
int x[f()];
constexpr int f() { return 100; }
21. cyberplus
constexpr in C++14
Enhancements:
• Almost any statement permitted, including decisions and loops
• Not a non-static const member function automatically
– Can change only objects created within the constant
context
• No goto statement
• No static or thread_local variables
• No local variables without initializers
22. cyberplus
Static Assertions
assert
Runtime check
static_assert
Compile time check
Pre-processor Compiler Runtime
#error static_assert assert
static_assert(condition, error_message);
static_assert(MAX>100, "MAX is too big!");
template <class T>
class A { static_assert(sizeof(T)>32, "Size too big!"); }
24. cyberplus
Enumeration in C++03
Problems:
1. Underlying type and size is platform dependent and non-portable
2. Enumerators expand into the scope of the enumeration
3. Easy conversion to integers makes undesirable possibilities
possible
4. Forward declarations are not possible
25. cyberplus
Enumeration in C++11
enum class Day {
Sun, Mon, Tue, Wed, Thu, Fri, Sat
};
Problem #1:
Underlying type and size is platform dependent and non-portable
Solution:
Underlying type is int (and can be changed by explicitly specifying),
and is therefore portable
enum class Day : unsigned int {
Sun, Mon, Tue, Wed, Thu, Fri, Sat };
enum Day : unsigned int {
Sun, Mon, Tue, Wed, Thu, Fri, Sat };
26. cyberplus
Enumeration in C++11
Problem #2:
Enumerators expand into the scope of the enumeration
Solution:
Enumerators are protected by the scope of the enumeration
enum class Day {
Sun, Mon, Tue, Wed, Thu, Fri, Sat };
enum class SolarSystem {
Sun, Earth, Moon };
Day d(Day::Tue);
if (d == Day::Wed) { /* ... */ }
27. cyberplus
Enumeration in C++11
Problem #3:
Easy conversion to integers makes undesirable possibilities possible
Solution:
Enumerators are not implicitly convertible to other types
enum class Day {
Sun, Mon, Tue, Wed, Thu, Fri, Sat };
enum class SolarSystem {
Sun, Earth, Moon };
if (Day::Sun == SolarSystem::Sun) { /* ... */ }
28. cyberplus
Enumeration in C++11
Problem #4:
Forward declarations are not possible
Solution:
Forward declarations are possible as the underlying type is known
enum Day; // Wrong! Can't determine underlying type
// without seeing enumerators
enum class Day; // Ok, type is int
enum class Day: char; // Ok, type is char
enum Day: char; // Ok, type is char
34. cyberplus
Smart Pointers: auto_ptr
void f()
{
int *i = new int;
// Don't forget to delete
delete i;
}
void f()
{
int *i = new int;
std::auto_ptr<int> p(i);
//Don't have to delete
}
A B
37. cyberplus
Smart Pointers: auto_ptr
int *i = new int(3);
std::auto_ptr<int> p1(i);
std::cout << *p1 << endl; // 3
std::cout << p1.get() << endl;// Address of the int
int *j;
j=p1.get();
std::cout << j << endl; // Address of the int
std::cout << p1 << endl; // Address of the int
38. cyberplus
Smart Pointers: auto_ptr
int *i = new int(3);
std::auto_ptr<int> p1(i);
std::cout << *p1 << endl; // 3
std::cout << p1.get() << endl;// Address of the int
int *j;
j=p1.release();
std::cout << j << endl; // Address of the int
std::cout << p1 << endl; // NULL
delete j; //Deallocates memory
39. cyberplus
Smart Pointers: auto_ptr
int *i = new int(3);
std::auto_ptr<int> p1(i);
std::cout << *p1 << endl; // 3
std::cout << p1.get() << endl;// Address of the int
p1.reset();
std::cout << p1.get() << endl; // NULL
40. cyberplus
Smart Pointers: auto_ptr
int *i = new int(3);
std::auto_ptr<int> p1(i);
std::cout << *p1 << endl; // 3
std::cout << p1.get() << endl;// Address of the int
p1.reset(new int(5));
std::cout << p1.get() << endl; // Address of the new int
std::cout << *p1 << endl; // 5
41. cyberplus
Smart Pointers: auto_ptr
int *i = new int(3);
std::auto_ptr<int> p1(i);
std::cout << *p1 << endl; // 3
std::cout << p1.get() << endl;// Address of the int
std::auto_ptr<int> p2;
p2=p1; // Ownership transferred to p2
std::cout << p1.get() << endl; // NULL
std::cout << p2.get() << endl; // Address of the int
42. cyberplus
Smart Pointers: unique_ptr
int *i = new int(3);
std::unique_ptr<int> p1(i);
std::cout << *p1 << endl; // 3
std::cout << p1.get() << endl;// Address of the int
std::unique_ptr<int> p2;
p2 = p1; // Error: Copy semantics not supported
p2 = std::move(p1); // Correct: Move semantics
std::cout << p1.get() << endl; // NULL
std::cout << p2.get() << endl; // Address of the int
43. cyberplus
Smart Pointers: shared_ptr
int *i = new int(3);
std::shared_ptr<int> p1(i);
std::cout << *p1 << endl; // 3
std::cout << p1.get() << endl;// Address of the int
std::shared_ptr<int> p2;
p2 = p1; // Both have ownership
std::cout << p1.get() << endl; // Address of the int
std::cout << p2.get() << endl; // Address of the int
p1.release(); // p2 still owns the raw pointer
p2.release(); // Memory freed
44. cyberplus
Smart Pointers: weak_ptr
int *i = new int(3);
std::shared_ptr<int> p1(i);
std::cout << *p1 << endl; // 3
std::cout << p1.get() << endl;// Address of the int
std::weak_ptr<int> wp1;
wp1 = p1; // p1 retains ownership
std::cout << p1.get() << endl; // Address of the int
std::cout << wp1.get() << endl; // Address of the int
std::shared_ptr<int> p2 = wp1.lock();
if (p2) {
// p1 and p2 own the raw pointer
}
49. cyberplus
Type Inference
int x=10;
auto y=10;
auto d = getCurrentDate();
int x[10];
for (auto i: x) { i; }
for (auto i=v.begin(); i!=v.end(); i++)
for (auto i: v) { i; }
for (auto &i: v) { i; }
for (auto &&i: v) { i; }
52. cyberplus
Improved Function Syntax
class A {
int add(int x, int y);
};
int A::add(int x, int y) {
return x+y;
}
class A {
auto add(int x, int y) -> int;
};
auto A::add(int x, int y) -> int {
return x+y;
}
C++14 (Needs to be defined before use)
auto A::add(int x, int y) {
return x+y;
}
53. cyberplus
Improved Function Syntax
template <class A, class B>
decltype(a+b) add(A &a, B &b) { return a+b; }
template <class A, class B>
auto add(A &a, B &b) -> decltype(a+b){ return a+b; }
54. cyberplus
Lambda Expressions
Syntax:
[capture] (parameters) -> return_type {function_body}
[](int x, int y) -> int { return x+y; }
int (*funcptr)(int,int);
funcptr = [](int x, int y) -> int { return x+y; };
std::cout << funcptr(2,3) << endl;
auto func = [](int x, int y) -> int { return x+y; };
std::cout << func(2,3) << endl;
55. cyberplus
Lambda Expressions
To print all odd numbers from a vector:
std::vector<int> v = {1,6,7};
std::for_each(begin(v), end(v),
[](int x){if (x%2==1) std::cout<<x<<endl; } );
56. cyberplus
Lambda Expressions
To print all odd numbers greater than 'n' from a vector:
std::vector<int> v = {1,6,7};
int n=5;
std::for_each(begin(v), end(v),
[n](int x){if (x%2==1 && x>n) std::cout<<x<<endl; } );
57. cyberplus
Lambda Expressions
To find the sum of all odd numbers in a vector:
std::vector<int> v = {1,6,7};
int sum=0;
std::for_each(begin(v), end(v),
[&sum](int x){if (x%2==1) sum+=x; } );
58. cyberplus
Lambda Expressions
To find the sum of all odd numbers and print all odd numbers greater than 'n':
std::vector<int> v = {1,6,7};
int sum=0;
int n=5;
std::for_each(begin(v), end(v),
[n,&sum](int x){
if (x%2==1) {
sum+=x;
if (x>n) std::cout << x << endl;
}
} );
std::cout << sum << endl;
59. cyberplus
Summary of Captures
Capture Meaning
[] No access to other variables
[a,b,c] Read-only access to specified variables only
[&a,&b] Read-write access to specified variables only
[a,&b,&c,
d]
Read-only access to a and d; read-write access to b
and c
[&] Read-write access to all variables
[=] Read-only access to all variables
[&,a,b,c] Read-write access to all variables; read-only access
to specified variables
[=,&a,&b] Read-only access to all variables; read-write access
to specified variables
69. cyberplus
Initializer Lists
class Point {
int x,y;
std::vector<int> values;
public:
Point(std::initializer_list<int> l): values(l) {
x = values[0];
y = values[1];
}
};
Point p1 = {2,3};
70. cyberplus
Initializer Lists
class Point {
int x,y;
public:
Point(int x, int y) {
this->x = x;
this->y = y;
// Anything else can go here
}
};
Point p1 = {2,3};
71. cyberplus
Initializer Lists
Point p = {2,3};
Point p{2,3};
Point *p = new Point{2,3};
Point getPoint(int i) {
return Point{x[i], y[i]};
}
Point getPoint(int i) {
return {x[i], y[i]};
}
Point(int x, int y): x{x}, y{y} {
}
72. cyberplus
Initializer Lists
class Point {
int x,y;
std::vector<int> values;
public:
void setPoint(std::initializer_list<int> l) {
std::initializer_list<int>::iterator i;
i = l.begin();
x = *i++;
y = *i;
}
};
Point p;
p.setPoint({2,3});
77. cyberplus
Base Class Constructors
class A {
int x;
public:
A(int x): x{x} {}
};
class B: public A {
public:
B(int x): A{x} {}
};
class A {
int x;
public:
A(int x): x{x} {}
};
class B: public A {
public:
using A::A;
};
Limitations:
1. All constructors are inherited
2. Derived class cannot have constructors with same signature as base class
3. Multiple base classes cannot have constructors with the same signature
80. cyberplus
Class declaration:
class A;
Template class declaration:
template class A<int>; // Instantiates
External template class declaration:
extern template class A<int>; // Does not instantiate
extern Templates