程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> builtin type也有析構函數?

builtin type也有析構函數?

編輯:C++入門知識

sgi stl 中有個全局函數,destroy,發現對一個指針參數進行調用時,僅有一個實現。如下:

wps_clip_image-4691

也就是說針對普通類型(如int,bool等等),也是調用的這個函數。

做了如下測試:

wps_clip_image-16510

確實可以如此簡寫。(其實是c++標准,為了簡化而提供的支持)

參考資料

It's the reason that makes your code work for generic parameters. Consider a container C:

template<typename T>
struct C {
// ...
    ~C() {
for(size_t i = 0; i<elements; i++)
            buffer[i].~T();
    }
};

It would be annoying to introduce special cases for built-in types. So C++ allows you to do the above, even if T happens to equal to int. The holy Standard says in 12.4 p15:

The notation for explicit call of a destructor can be used for any scalar type name. Allowing this makes it possible to write code without having to know if a destructor exists for a given type.

The difference between using a plain int and a typedef'ed int is that they are syntactically different things. The rule is, that in a destructor call, the thing after the ~ is a type-name. int is not such a thing, but a typedef-name is. Look it up in 7.1.5.2.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved