c++ 11 知识点

1. 万能引用:

int add(){
    return 100;
}

 const int &n = add(); // 编译通过

int &p = add(); // 不能编译通过

2. std::move

#include <iostream>
using namespace std;

class Moveable{
public:
    Moveable():i(new int(3)) {}
    ~Moveable() { delete i; }
    Moveable(const Moveable & m): i(new int(*m.i)) { }
    Moveable(Moveable && m):i(m.i) {
        m.i = nullptr; 
    }
    int* i;
};

int main() {
    Moveable a;

    Moveable c(move(a));    // 会调用移动构造函数
    cout << *a.i << endl;   // 运行时错误
}

 

3. 完美转发

#include <iostream>
using namespace std;

void RunCode(int && m) { cout << "rvalue ref" << endl; }
void RunCode(int & m) { cout << "lvalue ref" << endl; }
void RunCode(const int && m) { cout << "const rvalue ref" << endl; }
void RunCode(const int & m) { cout << "const lvalue ref" << endl; }

template <typename T>
void PerfectForward(T &&t) {   RunCode(forward<T>(t));    }

int main() {
    int a;
    int b;
    const int c = 1;
    const int d = 0;

    PerfectForward(a);          // lvalue ref
    PerfectForward(move(b));    // rvalue ref
    PerfectForward(c);          // const lvalue ref
    PerfectForward(move(d));    // const rvalue ref
}

4. auto 细节

int x;
int * y = &x;
double foo();
int & bar();

auto * a = &x;      // int*
auto & b = x;       // int&
auto c = y;         // int*
auto * d = y;       // int*
auto * e = &foo();  // 编译失败, 指针不能指向一个临时变量
auto & f = foo();   // 编译失败, nonconst的左值引用不能和一个临时变量绑定
auto g = bar();     // int
auto & h = bar();   // int&

5. auto  const  volatile 不能带着传递

double foo();
float * bar();

const auto a = foo();       // a: const double
const auto & b = foo();     // b: const double&
volatile auto * c = bar();  // c: volatile float*

auto d = a;                 // d: double
auto & e = a;               // e: const double &
auto f = c;                 // f: float *
volatile auto & g = c;      // g: volatile float * &

6. auto 与 decltype 区别

auto 初始化时定义

decltype 是以表达式出现的,可以用于程序运行时使用,也可以定义在宏里作类型定义

 

// 判断类型是否是一样的,在运行时判断

cout << is_same<decltype(pf), decltype(pf1)>::value << endl;    // 1

7. 强类型的枚举 (简化使用,否则太长)

#include <iostream>
using namespace std;

enum class Type { General, Light, Medium, Heavy };
enum class Category { General = 1, Pistol, MachineGun, Cannon };

int main() {
    Type t = Type::Light;
    t = General;                    // 编译失败,必须使用强类型名称
    if (t == Category::General)     // 编译失败,必须使用Type中的General
        cout << "General Weapon" << endl;
    if (t > Type::General)          // 通过编译
        cout << "Not General Weapon" << endl;
    if (t > 0)                      // 编译失败,无法转换为int类型
        cout << "Not General Weapon" << endl;
    if ((int)t > 0)                 // 通过编译
        cout << "Not General Weapon" << endl;
    cout << is_pod<Type>::value << endl;        // 1
    cout << is_pod<Category>::value << endl;    // 1
    return 0;
}

8. lambda 表达式

[捕获](参数列表)修饰   -> 返回值

int b;

int a;

auto fa = [=](int c) ->{ return a += b + c ;  };

[var] 传值方式。

[=] 父作用域的所有变量包括this。

[&var] 以引用的方式传递。

[&] 以引用的方式传递父作用域的变量,包括this。

[this] 以值方式传this指针。

a. lambda 函数是const 的所以直接修改会报错

int val;

auto mb = [=]() { val = 3; }; // 编译不能通过

auto mb = [=]() mutable { val = 3; } // 可以编译通过

 

b. lambda 的作用域只有父作用域,但是仿函数是可以正常的使用作用域的,这一点是写程序时要注意的。


int g_d = 3; // 全局的变量

int myfun(){
    auto fun = [g_d]{}; // 不能编译过;
}

 

      9 bind

#include <iostream>
#include <functional>
 
void fn(int n1, int n2, int n3) {
	std::cout << n1 << " " << n2 << " " << n3 << std::endl;
}
 
int fn2() {
	std::cout << "fn2 has called.\n";
	return -1;
}
 
int main()
{
	using namespace std::placeholders;
	auto bind_test1 = std::bind(fn, 1, 2, 3);
	auto bind_test2 = std::bind(fn, _1, _2, _3);
	auto bind_test3 = std::bind(fn, 0, _1, _2);
	auto bind_test4 = std::bind(fn, _2, 0, _1);
 
	bind_test1();//输出1 2 3
	bind_test2(3, 8, 24);//输出3 8 24
	bind_test2(1, 2, 3, 4, 5);//输出1 2 3,4和5会被丢弃
	bind_test3(10, 24);//输出0 10 24
	bind_test3(10, fn2());//输出0 10 -1
	bind_test3(10, 24, fn2());//输出0 10 24,fn2会被调用,但其返回值会被丢弃
	bind_test4(10, 24);//输出24 0 10
	return 0;

————————————————
版权声明:本文为CSDN博主「___Blue_H」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_37653144/article/details/79285221

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值