C++ 11 Features and training notes
C++ 11 features
Rule of 5
-Destructor
-Copy construtor
-Copy assignment
-Move constructor
-Move assignment
------------------------------------------------------------------
------------------------------------------------------------------
Range based for loop
for (declaration: expression){
// do some loop stuff
}
// Arrays
int arr[] = {1,2,3,4,5};
for (int& e:arr){ //& Lvalue reference
cout << e <<endl;
}
// using auto keyword
for(auto i : v)
std::cout <<i<<";
//using rvalue refence
for(auto&& i : V)
std::cout<<i<<";
------------------------------------------------------------------
Null pointer
NULL is not keyword
#define included in standard libraries
int main(){
int*i=NULL;
}
leads to error error:use of undeclared identifier 'NULL'
in C++ 11 nullptr is a new keyword
it has its own type std::nullptr_t
it is implicitly convertible to any pointer
it is not impicitly convertible to integer or string
------------------------------------------------------------------
Strongly typed enum
enum class Selection : int{
None,
Single,
Multiple
};
Selectionsel = Seleection::Single;
------------------------------------------------------------------
Lambda expressions/functions
anonymouus function
Functions that are not going to be reuse
#include <iostream>
using namespace std;
int main(){
[]{}; //lambda definition
[]{}(); // lambda invocation
}
#include <iostream>
using namespace std;
int main(){
int count = 10;
[count]{cout<<"Count:"<<count<<endl;}();
}
output :
Count:10
//capture all @TODO
#include <iostream>
using namespace std;
int main(){
int count = 10;
int next = 11;
[=]{cout<<"Count:"<<count<<endl;
cout<<"Next:"<<next<<endl;}();
}
//capture by reference @TODO
#include <iostream>
using namespace std;
int main(){
int count = 10;
[&count]{cout<<"Count:"<<count<<endl;
count =20;
}();
cout <<
}
------------------------------------------------------------------
Automatic type detection during initialization
auto i =10; //i is an int
auto l =10LL; // L is an long long
auto p = new Instrument(); // p is a pointer to instrument
auto foo() -> int
{
return 42;
}
template <typename T>
auto foo (Tvalue)-->deltype(value.bar())
{
return value.bar()
}
C++ 14 makes the trainling return type unnecessary
auto foo()
{
return 42;
}
------------------------------------------------------------------
Smart pointers
Pointer capable of automatically deleting the heap memory allocated when it goes out of scope
1)Unique Pointers
--> Light weight smart pointers
--> Unique _pptr object cannot be copied
--> Be cautious while using release
2) Shared Pointers
------------------------------------------------------------------
Lvalue & Rvalue
lvalue has a name or memory address associate to it.
--> &x ( gives idnetifiable memory address)
x = 10
| |
lvalue Rvalue
Lvalue reference
int var = 9
int &var_new = var; (Lvalue reference takes lvalue)
Rvalue is the right hand side of the assignment.
--> function return value is Rvalue
Rvalue reference
Std::move converts an lvalue to rvaluve
Move constructors
Move assignments
------------------------------------------------------------------
Pertfect forwarding
--> pass as it is
Std::forward<T>
Comments
Post a Comment