C++11 | 新增特性【常用系列】

9 篇文章 2 订阅

一、关键字

1.1 alignas

  • func:指定符可应用到变量非位域类数据成员的声明、class/struct/union枚举的定义。不能应用于参数
  • 注意:但弱于原应有的对齐情况;
struct alignas(8) S {};
struct alignas(1) U { S s; }; // 错误:若无 alignas(1) 则 U 的对齐将为 8

1.2 alignof

  • func:获取内存对齐代替__alignof__;可用于完整类型、数组类型或者引用类型;
#include <iostream>
 
struct Foo {
    int   i;
    float f;
    char  c;
};
 
struct Empty {};
 
struct alignas(64) Empty64 {};
 
int main()
{
    std::cout << "Alignment of"  "\n"
        "- char             : " << alignof(char)    << "\n"
        "- pointer          : " << alignof(int*)    << "\n"
        "- class Foo        : " << alignof(Foo)     << "\n"
        "- empty class      : " << alignof(Empty)   << "\n"
        "- alignas(64) Empty: " << alignof(Empty64) << "\n";
}

1.3 decltype

  • func:编译时,对该参数进行类型推导,只返回该参数的类型;
    • 结合auto追踪函数的返回值;
      auto multiply(_Tx x, _Ty y)【函数名】->decltype(_Tx*_Ty)
    • 重新使用匿名结构体;decltype(结构体名称) 结构体新名称
    • 结合typedef/using能定义类型;
      using size_t = decltype(sizeof(0))
      typedef decltype(变量) 类型名

1.4 auto

  • func:变量的自动类型判断;
  • 一般用于代替冗长复杂变量使用范围专一的变量声明;
int a = 10;
auto au_a = a;	//自动类型推断,au_a为int类型
cout << typeid(au_a).name() << endl;

1.5 static_assert

  • func:静态断言 - 编译器期断言;之前接触的assert是运行期断言;
static_assert(常量表达式,"提示语");

1.6 noexcept

  • func:进行编译时检查,若表达式声明为不抛出任何异常则返回true;
    void func() noexcept;

1.7 constexpr

  • func:constexpr 指定符声明可以在编译时求得函数或变量的值;
  • 要求:其类型必须是字面类型,它必须被立即初始化,其初始化的完整表达式,包括所有隐式转换、构造函数调用等,都必须是常量表达式
int i;
const int size = i;
int arr[size];         //error,size不是常量表达式,不能在编译期确定

constexpr auto size = 10;
int arr[size];                     //OK,size时常量表达式

int i;
constexpr int size = i;  // error,i不能在编译期确定
constexpr int foo(int i) {
    return i + 5;
}

int main() {
    int i = 10;
    std::array<int, foo(5)> arr; // OK,5是常量表达式,计算出foo(5)也是常量表达式
    
    foo(i); // Call is Ok,i不是常量表达式,但仍然可以调用(constexpr 被忽略)
    
    std::array<int, foo(i)> arr1; // Error,但是foo(i)的调用结果不是常量表达式了
   
}

1.8 thread_local

  • func:能够在线程中创建一个全局变量或对象的本地副本,可以避免多线程情形下的资源竞争
    线程存储期。对象的存储在线程开始时分配,而在线程结束时解分配。每个线程拥有其自身的对象实例。唯有声明为 thread_local 的对象拥有此存储期。能与 staticextern一同出现。
    static thread_local int thread_count = 1;

1.9 for

for(elem : list) {
	即可遍历元素
}

1.10 delete与default

在C++类中可使用以上两种来对函数成员的限制;

  • 在一个类种编译器提供了以下几种默认函数:
    • 默认构造函数
    • 默认析构函数
    • 默认拷贝构造函数
    • 默认赋值函数
    • 移动构造函数
    • 移动拷贝函数

=delete:禁用该函数;
=default:使用编译器提供的默认函数;

在这里插入图片描述

二、预定义宏

2.1 __STDC_HOSTED__

  • func:编译器的目标系统环境中,包含标准C库,宏定义为1,否则为0;

2.2 __STDC__

  • func:在C编译器中,表示编译器是否与C标准一致,C++编译器中由编译器决定这个宏是否定义,定义成什么值;

2.3 __STDC__VERSION__

  • func:在C编译器中,表示编译器支持的标准版本,C++编译器中由编译器决定这个宏是否定义,定义成什么值;

2.4 __STDC_ISO_10646__

  • func:用于表示C++编译环境符合某个版本的ISO/IEC 10646标准

2.5 __func__

  • func:返回所在函数的名字;
    const char *hello() { return __func__; }

2.6 _Pragma

  • func:_Pragma (字符串字面值) == #pragma once

2.7 __VA_ARGS__

  • func:可以在宏定义的实现部分,替换省略号所代表的字符串;
    #define PR(...) printf{__VA_ARGS__}

三、类相关

3.1 final

  • func:禁止子类重写父类的函数;
    void func() final

3.2 override

  • func:说明子类重写父类函数;
    void func() override;

3.3 继承构造函数

  • func:子类的构造函数可直接初始化父类;
#include <string>

class A
{
  public:
    A(int i){};
    A(std::string str){};

};

class B : A
{
  public:
    using A::A;
};

int main(int argc, char const *argv[])
{
    B b1(123); // ok
    B b2("abc"); // ok
    return 0;
}

四、其他

4.1 lambda

  • 格式:[capture list] (params list) mutable exception-> return type { function body }
    • capture list:捕获外部变量列表;
    • params list:形参列表;
    • mutable指示符:用来说用是否可以修改捕获的变量;
    • exception:异常设定;
    • return type:返回类型;
    • function body:函数体;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool cmp(int a, int b)
{
    return  a < b;
}

int main()
{
    vector<int> myvec{ 3, 2, 5, 7, 3, 2 };
    vector<int> lbvec(myvec);

    sort(myvec.begin(), myvec.end(), cmp); // 旧式做法
    cout << "predicate function:" << endl;
    for (int it : myvec)
        cout << it << ' ';
    cout << endl;

	// Lambda表达式
    sort(lbvec.begin(), lbvec.end(), [](int a, int b) -> bool { return a < b; });   
    cout << "lambda expression:" << endl;
    for (int it : lbvec)
        cout << it << ' ';
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jxiepc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值