-
-
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?
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> | |
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; | |
} | |
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(); | |
void *q = malloc(sizeof(int)); | |
int *i = new (q) int(); | |
i->~int(); // *** ERROR HERE. | |
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 *q = malloc(sizeof(int)); | |
int *i = new (q) int(); | |
DeleteIt(i); // *** No error anymore. | |
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> | |
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