Created
September 8, 2015 15:54
-
-
Save anonymous/279c6826de5cfc22c488 to your computer and use it in GitHub Desktop.
The first example's second case won't compile (complains it expected a class after ~) but the second one through the template does. Anyone know what I'm doing wrong?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
size_t size = malloc_size(p); | |
printf("after destructing size = %ld\n", size); | |
void *q = malloc(sizeof(int)); | |
int *i = new (q) int(); | |
i->~int(); | |
size = malloc_size(q); | |
printf("after destructing size = %ld\n", size); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 *p = malloc(sizeof(std::string)); | |
std::string *a = new(p) std::string(); | |
DeleteIt(a); | |
size_t size = malloc_size(p); | |
printf("after destructing size = %ld\n", size); | |
void *q = malloc(sizeof(int)); | |
int *i = new (q) int(); | |
DeleteIt(i); | |
size = malloc_size(q); | |
printf("after destructing size = %ld\n", size); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment