当前位置:编程学习 > C/C++ >>

builtin type也有析构函数?

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.

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,