constexpr int *pointer 和 const int *pointer 意义是不同的;前面表示的是常量指针,后面表示指针对象是常量;constexpr int *pointer 和 int * const pointer 意义相同
/**
* @Author: phd
* @Date: 2019/12/13
* @Site: github.com/phdsky
* @Description: NULL
*/#include<iostream>usingnamespace std;voidtest_1(){constint const_int =1024;// int &reference_int = const_int; // error: binding value of type 'const int' to reference to type 'int' drops 'const' qualifierconstint&const_reference_int = const_int;}voidtest_2(){double value_double =3.14;constint&const_reference_int = value_double;
cout <<"double val addr: "<<&value_double << endl;
cout <<"const int val addr: "<<&const_reference_int << endl;
cout <<"Note compiler produced a new const temp for const_reference_int to binding !"<< endl;// int &reference_int = value_double; // error: non-const lvalue reference to type 'int' cannot bind to a value of unrelated type 'double'}voidtest_3(){// referenceint value_int =42;int&reference_int = value_int;constint&const_reference_int = value_int;
reference_int =0;// const_reference_int = 0; // error: cannot assign to variable 'const_reference_int' with const-qualified type 'const int &'// pointerconstdouble const_double =3.14;// double *double_pointer = &const_double; // error: cannot initialize a variable of type 'double *' with an rvalue of type 'const double *'constdouble*const_double_pointer =&const_double;// const_double_pointer = 6.28; // error: assigning to 'const double *' from incompatible type 'double'}voidtest_4(){int value_int =233;constint const_value_int =2333;int*const int_const_pointer =&value_int;// int * const int_cosnt_pointer = &const_value_int; // error: cannot initialize a variable of type 'int *const' with an rvalue of type 'const int *'constint*const const_int_const_pointer =&const_value_int;// wanna to change pointer's point// const_int_const_pointer = &value_int; // error: cannot assign to variable 'const_int_const_pointer' with const-qualified type 'const int *const'// wanna to change value*int_const_pointer =666;// *const_int_const_pointer = 6666; // error: read-only variable is not assignable}constintreturn_const_value(){return2333;}constexprintreturn_consexpr_value(){return233333;}constexprint constexpr_int_outer =666;voidtest_5(){constint const_int =233;constint const_int_another = const_int +1;// Is a const expressionconstint const_int_other =return_const_value();// Is not a const expression, because function value can be obtained only after runningconstexprint constexpr_int =2333;constexprint constexpr_int_another = constexpr_int +1;// Is a const expressionconstexprint constexpr_int_other =return_consexpr_value();// Is a const expression
cout << const_int_other <<" "<< constexpr_int_other << endl;// constexpr int *constexpr_int_pointer = &constexpr_int; // error: cannot initialize a variable of type 'int *const' with an rvalue of type 'const int *'// int * const int_const_pointer = &constexpr_int; // error: same as aboveconstint* const_int_pointer =&constexpr_int;intconst* int_const_pointer =&constexpr_int;// fuck, they are the same// constexpr const int *const_int_constexpr_pointer = &constexpr_int; // error: constexpr variable 'const_int_constexpr_pointer' must be initialized by a constant expressionconstexprconstint*const_int_constexpr_pointer_another =&constexpr_int_outer;// must defined outer the function body}intmain(int argc,char*argv[]){test_1();test_2();test_3();test_4();test_5();return0;}