mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
prepare for c++17
This commit is contained in:
36
code/3/3.6.cpp
Normal file
36
code/3/3.6.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// 3.6.cpp
|
||||
// c++1x tutorial
|
||||
//
|
||||
// created by changkun at shiyanlou.com
|
||||
//
|
||||
// 完美转发
|
||||
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
void reference(int& v) {
|
||||
std::cout << "左值引用" << std::endl;
|
||||
}
|
||||
void reference(int&& v) {
|
||||
std::cout << "右值引用" << std::endl;
|
||||
}
|
||||
template <typename T>
|
||||
void pass(T&& v) {
|
||||
std::cout << "普通传参:";
|
||||
reference(v);
|
||||
std::cout << "std::move 传参:";
|
||||
reference(std::move(v));
|
||||
std::cout << "std::forward 传参:";
|
||||
reference(std::forward<T>(v));
|
||||
|
||||
}
|
||||
int main() {
|
||||
std::cout << "传递右值:" << std::endl;
|
||||
pass(1);
|
||||
|
||||
std::cout << "传递左值:" << std::endl;
|
||||
int v = 1;
|
||||
pass(v);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user