mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 04:34:40 +03:00
37
code/3/3.7.perfect.forward.cpp
Normal file
37
code/3/3.7.perfect.forward.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// 3.6.perfect.forward.cpp
|
||||
// modern c++ tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
// https://github.com/changkun/modern-cpp-tutorial
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
void reference(int& v) {
|
||||
std::cout << "lvalue reference" << std::endl;
|
||||
}
|
||||
void reference(int&& v) {
|
||||
std::cout << "rvalue reference" << std::endl;
|
||||
}
|
||||
template <typename T>
|
||||
void pass(T&& v) {
|
||||
std::cout << " normal param passing: ";
|
||||
reference(v);
|
||||
std::cout << " std::move param passing: ";
|
||||
reference(std::move(v));
|
||||
std::cout << " std::forward param passing: ";
|
||||
reference(std::forward<T>(v));
|
||||
std::cout << "static_cast<T&&> param passing: ";
|
||||
reference(static_cast<T&&>(v));
|
||||
}
|
||||
int main() {
|
||||
std::cout << "rvalue pass:" << std::endl;
|
||||
pass(1);
|
||||
|
||||
std::cout << "lvalue pass:" << std::endl;
|
||||
int l = 1;
|
||||
pass(l);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user