D
Dave Theese
Please see questions in the code below... Thanks!
#include <iostream>
using namespace std;
class foo_class
{
public:
foo_class() {cout << "Constructing..." << endl;}
~foo_class() {cout << "Destructing..." << endl;}
};
template <typename T>
void foo()
{
unsigned char mem[100];
T *ptr;
ptr = new(mem) T;
ptr->~T(); // Funky destructor call - "concatenating" '~' with T's value.
// In an overly-simplified sense, templates are a text
// substitution mechanism, but this use of a template
parameter is
// different than what is usually seen. Is this really legal?
}
int main()
{
foo<foo_class>(); // Outputs "Constructing...\nDestructing...\n"
// Is my compiler exhibiting correct behavior? Should
this
// be legal (given what happens inside foo())?
foo<int>(); // This works too! How is that happening? What does it mean
to
// "destruct" an int (which is what happens inside foo())???
return 0;
}
#include <iostream>
using namespace std;
class foo_class
{
public:
foo_class() {cout << "Constructing..." << endl;}
~foo_class() {cout << "Destructing..." << endl;}
};
template <typename T>
void foo()
{
unsigned char mem[100];
T *ptr;
ptr = new(mem) T;
ptr->~T(); // Funky destructor call - "concatenating" '~' with T's value.
// In an overly-simplified sense, templates are a text
// substitution mechanism, but this use of a template
parameter is
// different than what is usually seen. Is this really legal?
}
int main()
{
foo<foo_class>(); // Outputs "Constructing...\nDestructing...\n"
// Is my compiler exhibiting correct behavior? Should
this
// be legal (given what happens inside foo())?
foo<int>(); // This works too! How is that happening? What does it mean
to
// "destruct" an int (which is what happens inside foo())???
return 0;
}