📖
Wiki
Back to my personal website
  • Home
  • Equipment and Devices
    • 3D Printer
    • Laser Cutter
    • Motion Capture System
    • Sensors
      • RGB-D Cameras
      • Velodyne LiDAR
      • Zed Camera
      • RealSense D435i
      • IMU
    • eGPU
    • Nvidia AGX Xavier
    • CPU Benchmark
    • Installation Checklist
  • Development
    • Linux
      • Shell
      • GDB
      • Git
      • Tmux
      • Network
      • Tricks
      • Debug FAQ
    • CMake
      • Catkin Tools
      • CMakeLists
      • CMake Variables
      • CMake Commands
      • CMake: find_package()
    • ROS
      • Gazebo
      • wstool
      • roslaunch
      • rosbag
      • multi-threaded spinner
    • ROS2
      • Convert ROS1 bag to ROS2 bag
    • C++
      • C++ 11
      • C++ Examples
      • C++ Debug
      • Factory Method
      • Timing
    • Google Tools
      • GLog
      • GFlags
      • GTest
      • Style Guide
      • Clang Format
    • PCL
      • Point Type
      • Methods
      • Architecture
      • Code Explained
    • Open3D
      • Python API
      • Registration
      • Visualization
      • Tools
    • OpenCV
      • Documentation
      • Modules
    • Other Libraries
      • Eigen
      • Ceres
      • g2o
      • GTSAM
    • Website
  • Algorithm
    • SLAM
      • K-D Tree
      • Octree
      • Bag of Words
      • Distance Measures
      • Coordinate Systems
      • LOAM
      • Iterative Closest Point
      • Generalized ICP
      • Mahalanobis Distance
    • Computer Science
      • Computational Model
      • Sorting
      • Analysis
      • Complexity Classes (P, NP)
      • Divide and Conquer
      • Greedy Algorithm
      • Dynamic Programming
      • Tree
      • Graph
    • Computer Vision
      • Camera Models
      • Distortion
      • Motion Models
      • Shutter
      • Image Sensors
      • Epipolar Geometry
      • Multiple-View Geometry
    • Datasets
      • RGB-D Datasets
      • Point Cloud Datasets
      • LiDAR SLAM Datasets
  • Math
    • Optimization
      • Convex Optimization
      • Descent Methods
    • Probability
      • Moment
      • Covariance Matrix
      • Stochastic Process
    • Topology
      • References
      • Concepts
      • Topological Spaces
      • Representation of Rotations
      • Representation of 3-sphere
    • Algebra
      • Linear Algebra
      • Matrix Factorization
      • Condition Number
      • Matrix Lie Group
    • Differential Geometry
      • Manifold
      • Submanifold
      • Quotient Manifolds
      • Tangent Space
  • Quadrotor
    • PX4 Development
    • Companion Computer
    • Drone Hardware
    • Propeller Lock
    • Debug
  • Favorites
    • Bookmarks
Powered by GitBook
On this page
  • C++ 11
  • auto
  • initialization
  • for loop
  • tuple (variable-length template)
  • lambda expression
  • decltype
  • nullptr
  • rvalue reference; move semantics; perfect forwarding

Was this helpful?

  1. Development
  2. C++

C++ 11

C++ 11

auto

auto a; // wrong
auto i = 1; 
auto d = 1.0; 
auto str = "Hello World"; 
auto ch = 'A'; 
auto func = less<int>(); 
vector<int> iv; 
auto ite = iv.begin();
auto p = new foo() // customized type

initialization

int arr[3]{1, 2, 3}; 
vector<int> v{1, 2, 3}; 
map<int, string> m{{1, "a"}, {2, "b"}};
string str{"Hello World"};

for loop

map<string, int> m{{"a", 1}, {"b", 2}, {"c", 3}}; 
for (auto p : m){ 
    cout<<p.first<<" : "<<p.second<<endl;
}

tuple (variable-length template)

auto p = make_pair(1, "before C++ 11");

auto t1 = make_tuple(1, 2.0, "C++ 11"); 
auto t2 = make_tuple(1, 2.0, "C++ 11", {1, 0, 2});

lambda expression

vector<int> iv{5, 4, 3, 2, 1}; 
int a = 2, b = 1;
// syntax: [global param](func args)->return_type{func body} 
for_each(iv.begin(), iv.end(), [b](int &x){cout<<(x + b)<<endl;});
for_each(iv.begin(), iv.end(), [=](int &x){x *= (a + b);});
for_each(iv.begin(), iv.end(), [=](int &x)->int{return x * (a + b);});

decltype

template <typename Creator> 
auto processProduct(const Creator& creator) -> 
decltype(creator.makeObject()) { 
    auto val = creator.makeObject(); 
    // do something with val
}

nullptr

void F(int a){
    cout << a << endl;
} 
void F(int *p){
    assert(p != NULL); 
    cout << p << endl;
} 
int main(){
    int *p = nullptr; 
    int *q = NULL; 
    int a = nullptr;  // compilation error; not allowed
    bool equal = ( p == q );  // true
    F(0);  // compilation error in C++98 due to ambiguity; call F(int) in C++11
    F(nullptr);
    return 0;
}

rvalue reference; move semantics; perfect forwarding

#include <iostream> 
#include <utility> 
void reference(int& v) { 
    std::cout << "lvalue" << std::endl;
} 
void reference(int&& v) { 
    std::cout << "rvalue" << std::endl;
} 
template <typename T> 
void pass(T&& v) { 
    std::cout << "regular: "; 
    reference(v); 
    std::cout << "std::move: "; 
    reference(std::move(v)); 
    std::cout << "std::forward: "; 
    reference(std::forward<T>(v)); // cf. static_cast<T&&>(v)
} 
int main() { 
    std::cout << "pass rvalue:" << std::endl; 
    pass(1);
    
    std::cout << "pass lvalue:" << std::endl; 
    int v = 1; 
    pass(v);
    
    return 0;
}
/* Output:
pass rvalue:
regular: lvalue
std::move: rvalue
std::forward: rvalue
pass lvalue:
regular: lvalue
std::move: rvalue
std::forward: lvalue
*/
PreviousC++NextC++ Examples

Last updated 3 years ago

Was this helpful?