What & How & Why

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
后一修订版
前一修订版
cs:programming:cpp:courses:cpp_basic_deep:chpt_2 [2024/04/16 14:23] – [decltype] codingharecs:programming:cpp:courses:cpp_basic_deep:chpt_2 [2024/04/16 15:09] (当前版本) – [对象生命周期] codinghare
行 400: 行 400:
 ==decltype== ==decltype==
 decltype 获取一个表达式,并返回表达式的类型。decltype 与 auto 的区别在于,decltype **不会产生类型退化**。 decltype 获取一个表达式,并返回表达式的类型。decltype 与 auto 的区别在于,decltype **不会产生类型退化**。
- * decltype(val):''val'' 代表 entity(**变量名称**),那么 ''val'' 是什么类型,那么返回的就是什么类型+  
 +   * decltype(val):''val'' 代表 entity(**变量名称**),那么 ''val'' 是什么类型,那么返回的就是什么类型
 <code cpp> <code cpp>
 //x is an variable name //x is an variable name
行 441: 行 442:
 int main() int main()
 { {
 +    //int
     std::integral auto z = 3;     std::integral auto z = 3;
 +    //error, 3.5 is not an integral
 +    std::integral auto z = 3.5;
 } }
 </code> </code>
 +====域和对象生命周期====
 +===域 Scope===
 +域代表了程序的一部分,域中的 name 有**唯一的含义**:
 +  * 全局域:程序最外部的域,全局对象
 +  * 块域:大括号限定的域,局部对象
 +  * 其他类型的域:Namespace, class 等等
 +域可以进行嵌套,内部域中的 Name 会掩盖外部域中的 Name:
 +<code cpp>
 +int x = 3;
 +int main()
 +{
 +   int x = 4;
 +   //call local x
 +   std::cout << x << '\n';
 +}
 +</code>
 +===对象生命周期===
 +生命周期指对象从被**初始化到被销毁**的区间。
 +  * 全局对象:生命周期为程序的运行期
 +  * 局部对象:起始于**初始化**,结束于域的执行完成
 +