1. value.cpp (C++11完美转发例子)
#pragma GCC diagnostic error "-std=c++11"
#include <iostream>
using namespace std;
void fun(int &x) { cout << "lvalue ref" << endl; }
void fun(int &&x) { cout << "rvalue ref" << endl; }
void fun(const int &x) { cout << "const lvalue ref" << endl; }
void fun(const int &&x) { cout << "const rvalue ref" << endl; }
template<typename T>
void PerfectForward(T &&t) { fun(std::forward<T>(t)); }
int main(int argc, char* argv[])
{
PerfectForward(10); // rvalue ref
int a;
PerfectForward(a); // lvalue ref
PerfectForward(std::move(a)); // rvalue ref
const int b = 8;
PerfectForward(b); // const lvalue ref
PerfectForward(std::move(b)); // const rvalue ref
system("pause");
return 0;
}
2.makefile
CXXFLAGS += -std=c++11
cc = g++
objects = value.o
exe=value
$(exe):$(objects)
$(cc) -o value $(objects)
value.o:value.cpp
.PHONY:clean
clean:
rm value $(objects)
g++ 方式1:
g++ -c value.cpp // 编译生成.o文件
g++ -o value value.o //链接生成目标文件
g++方式2:
g++ -o value value.cpp // 直接编译链接一起执行生成目标文件
makefile方式:
在makefile目录执行 :make
在生成文件目录执行: ./value
makefile可以参考: