一、关键字
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 的对象拥有此存储期。能与static
、extern
一同出现。
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 << ' ';
}