nullptr 和constepr
(1)nullptr
问题
#include<iostream>
using namespace std;
void fun(char *a){
cout<<"char"<<endl;
}
void fun(int a){
cout<<"int"<<endl;
}
int main(){
foo(null); //实际输出:int
foo(nullptr); //实际输出:char
reutn 0;
}
null错误原因:
宏定义,null 相当于 0;
nullptr
-
表示空指针,本质上还是0,但是指针类型的
-
foo(nullptr)等价于foo(char*(null))
-
当出现两个都是指针类型的参数时,会出现二义性,报错
(2)constexper
定义
定义常量函数的,用此定义的函数可以作为常量使用,比如:数组大小
特性
有时不允许 if else 语句
时期:
- 编译期 .cpp–>编译器—> exe
- 运行期 exe执行
#define LEN 10
int len_en(){
return 5;
}
comstexpr int len_mm(){
return 5;
}
int main(){
//局部变量,在栈上
int a[10];
int b[LEN];
int c1[len_en()]; //错误
int c2[len_mm()]; //正确
int len = 5;
int d[len]; //有些编译器不允许
}