Skip to content

Instantly share code, notes, and snippets.

@uliwitness
Forked from anonymous/int_doesnt_work.cpp
Last active September 8, 2015 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uliwitness/f5d3aeb6ccefd344d341 to your computer and use it in GitHub Desktop.
Save uliwitness/f5d3aeb6ccefd344d341 to your computer and use it in GitHub Desktop.
The second example's second case won't compile (complains it expected a class after ~) but the third one through the template does. Anyone know what I'm doing wrong?
#include <string>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <malloc/malloc.h>
class JollyRoger
{
public:
int mFoo;
};
using namespace std;
int main()
{
void *p = malloc(sizeof(JollyRoger));
JollyRoger *a = new(p) JollyRoger();
a->~JollyRoger(); // Compiles just fine even though neither int nor JollyRoger have a constructor or destructor.
return 0;
}
#include <string>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <malloc/malloc.h>
using namespace std;
int main()
{
void *p = malloc(sizeof(std::string));
std::string *a = new(p) std::string();
a->~string();
void *q = malloc(sizeof(int));
int *i = new (q) int();
i->~int(); // *** ERROR HERE.
return 0;
}
#include <string>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <malloc/malloc.h>
using namespace std;
template <class T>
void DeleteIt(T *x) { x->~T(); }
int main()
{
void *q = malloc(sizeof(int));
int *i = new (q) int();
DeleteIt(i); // *** No error anymore.
return 0;
}
#include <string>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <malloc/malloc.h>
typedef int INT;
using namespace std;
int main()
{
void *q = malloc(sizeof(INT));
INT *i = new (q) INT();
i->~INT();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment