c++ c++-faq copy-constructor assignment-operator rule-of-three
我遇到了以下代码,我无法理解它的含义:
typedef int INT;
5 .INT::~INT();
Run Code Online (Sandbox Code Playgroud)
注意:数字5和小数之间有一个空格.
问题:
1.有人可以解释上面两条线的确切含义吗?2.如果没有typedef,为什么它不起作用?它背后的任何原因?5 .int::~int()抛出错误.
我刚刚发现下面的代码是不是有效的C++(它不会在解析int后~):
int x = 5;
x.~int();
Run Code Online (Sandbox Code Playgroud)
但是,以下代码段确实有效:
int32_t x = 5;
x.~int32_t();
Run Code Online (Sandbox Code Playgroud)
这是因为int32_t是typedef在我的特定实现C++的,和析构函数,显然,可以在任何Typedef的类型调用.
我的问题是:是否需要允许第二个剪切编译的C++的任何实现?特别是,int32_t保证是一个typedef,并且如果它知道typedef typedef要int的话,是否允许销毁typedef所需的编译器?
请考虑以下代码:
#include <iostream>
typedef int t;
t a=42;
int main()
{
a.t::~t();
std::cout << a; //42
}
Run Code Online (Sandbox Code Playgroud)
我预计a会被摧毁.但事实并非如此,为什么?伪析构函数调用将如何销毁该对象?
c++ destructor primitive-types language-lawyer explicit-destructor-call
由于一致性原因,AFAIK,C++为原始类型提供析构函数.但这不适用于bool类型.
bool* vptr;
vptr->~bool(); // Error. "Expected a class name after '~' to name a destructor"
int8_t* vptr;
vptr->~int8_t(); // No error.
Run Code Online (Sandbox Code Playgroud)
我的代码出了什么问题?这是我的编译器版本.
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
Run Code Online (Sandbox Code Playgroud)