C++ 11/14/17/20 new feature
Created|Updated
|Post Views:
自从C++11以来的新特性
- auto 自动类型推导
- constexpr
- nullptr 关键字, 用于取代 NULL, 代表空指针
- for 循环中的 语法糖 for ( a : array) { }
- lambda 表达式, 也就是匿名函数
自从C99以来的新特性
- 变长数组 VLA;
- 结构体 按成员初始化
- restrict 关键字
- bool 类型
Author: wang shaodong
Copyright Notice: All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.
Related Articles
2020-02-15
C 语言中的main函数
C语言中main写法12345678910111213141516171819202122232425// 接受任意参数但不起作用int main(){ return 0;}// 明确不接受任何参数int main(void){ return 0;}// argc 参数的个数, argv 指向参数字符的首地址int main(int argc, char **argv){ return 0;}// 同上,第二个参数的不同写法int main(int argc, char *argv[]){ return 0;}// 此版本可以打印出环境变量int main(int argc, char *argv[], char *env){ return 0;} 关于main函数 main函数并非必须存在,只是约定俗成的一个程序启动入口罢了,main函数被crt(C语言运行时)调用可以通过编译器指令来替换入口函数 12345678// entry.c#includ...
2020-02-23
About preprocess
预处理是C/C++ 编译过程中的第一个环节, 处理以#开头的文本行 比如免费版和付费版的功能多少的条件编译 实现代码对于不同平台的兼容,或者对相同平台的不同版本进行兼容 宏的作用常见的就是简单的文本替换功能,实现函数的替换,DEBUG版本加打印,release 版本不加打印12345678910111213141516171819202122232425262728// 此代码来自 Learn C the hard way 一书#ifndef __dbg_h__#define __dbg_h__#include <stdio.h>#include <errno.h>#include <string.h>#ifdef NDEBUG#define debug(M, ...)#else#define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)#endif#define...
2020-01-20
About inline function
inline 是C99/C++的关键字之一,目的是取代函数式宏,函数式宏没有类型 内联函数 vs 函数式宏(待考证,可能有谬误) 编译中的差别: 内联函数 在编译时在调用函数中内敛只是是一种建议(像regester 一样),不一定被编译器采纳(The inline specification is only a request to the compiler. The compiler maychoose to ignore this request – – C++ primer),如果内敛函数很复杂(循环嵌套比较多),或者包含静态变量等情况就无法内敛就无法被编译器当做是内联函数 函数式宏 在预处理阶段被文本替换,在预处理阶段必然被替换 二进制的区别: 函数式宏 如果被使用多次 代码区增大 内联函数 如果被使用多次 代码区域没有显著增大 运行时的差别: 函数式宏,不是函数,在运行时没有体现 内联函数,几乎没有函数参数传递的开销 不同编译器对 inline 的支持 clang挖坑 gcc挖坑
2020-01-24
About sizeof
sizeof 的误解,sizeof 不是函数(function),是运算符(operator) 函数在程序运行的时候起作用,运算符求出来的大小是在编译时就确定下来的 常见的sizeof操作 对结构体类型求大小 1234567891011typedef struct { char a; int b; char *ptr; double c; char name[19];} Demo;// 结构体的实际所占用内存大小,需要内存对齐,按照4byte或者8byte对齐,// 对齐的原则整体大小能被4整除,而且每个成员的地址偏移都是4的整数倍std::cout << sizeof(Demo) << std::endl; 对联合体求大小 1234567891011#include<stdio.h>// 联合体的大小是能够容纳最大的成员的大小, 还要考虑一下四字节对齐typedef union { int a[3]; char b[8] ;} data;int main()...
2020-02-24
C++ VS c
C++ 曾经被叫做C With class, C++ 是兼容C的,并不是100%兼容 C++ 中的C子集是C++的一部分,C语言中允许的东西到C++中可能会不允许 全局变量初始化: C语言中只能使用 constant初始化全局变量 C++可以通过函数初始化 123456789101112131415#include <stdio.h>int init(){ printf("inside init func\n"); return 100;}int global_int = init(); // 此语句cpp文件能够编译执行,c文件编译失败int main(){ printf("Hello world\n"); printf("global_int= %d\n", global_int); return 0;} C++的C子集 是更好的C,将C中的一些不严格的编译检查,变得相对更严格C++ 编译器编译之后函数的 mangling n...
2020-05-23
namespace
namespace (命名空间) 是C++的一个关键字 不同的命名空间中相同的标识符在编译的时候不会产生错误 在不同的命名空间中可以使用相同名字的变量或者函数名称 C 语言的标识符 也有命名空间 (内容来自 C语言规范草案 6.2.3 节) label names标签名 包括 goto 语句 中用到的标签和 switch case中的 标签 the tags name of structures, unions and enumurations结构、联合、枚举的tag名 the members of structures or unions; each structure or union has a seperate name space for its number;结构体、联合的成员名 all other identifiers,called ordinary identifiers (declared in ordinary declarators or asenumeration constants)其他普通的变量,或者枚举常量 C++中使用namesapce ...
Announcement
This is my Blog