V
Virchanza
Let's say I have a struct as follows:
And let's say I create a global object of it which has all of its
members (including the intrinsic-type members) default-initialised:
Half-way through my program's execution, I want to "reset" this global
object. By "reset", I mean I want all of its member to go back to the
state that they were in when the "g_obj" object was initially created.
I achieve this with the following function:
This works fine for a struct/class object. However, I want this
template function to work for any kind of object, be it an intrinsic
or an array.
The problem with intrinsics or arrays is that you can't invoke their
destructor with the usual syntax (hence my template function will give
a compiler error).
Here's an idea I have in mind:
The Standard has the following to say:
The above quote only applies to POD-struct objects. If my Container
class contains a non-POD member, then the Standard doesn't guarantee
that the address of a Container object is the same as the address of
its first member. However, given that my Container class only contains
1 member, it's quite likely that the two addresses will be equal.
The only way I can imagine my template function behaving unexpectedly
is if the implementation stores some sort of "hidden data" within the
Container type (perhaps some debugging information). Possible
scenarios are:
1) Padding or debugging information at the beginning of the Container
(which is permitted by the Standard if the Container Class is a non-
POD)
2) Padding or debugging information at the end of the Container (which
is permitted by the Standard for both POD's and non-POD's)
Anyway...
Anyone got any ideas on how to Reset any kind of object?
Any opinions?
Code:
struct MyStruct{
int a;
double b;
std::string str;
};
And let's say I create a global object of it which has all of its
members (including the intrinsic-type members) default-initialised:
Code:
MyStruct g_obj = MyStruct();
Half-way through my program's execution, I want to "reset" this global
object. By "reset", I mean I want all of its member to go back to the
state that they were in when the "g_obj" object was initially created.
I achieve this with the following function:
Code:
template<class T>
void ResetObject(T &obj)
{
obj.~T(); /* Destruct */
::new(&obj) T(); /* Construct */
}
This works fine for a struct/class object. However, I want this
template function to work for any kind of object, be it an intrinsic
or an array.
The problem with intrinsics or arrays is that you can't invoke their
destructor with the usual syntax (hence my template function will give
a compiler error).
Here's an idea I have in mind:
Code:
template<class T>
void ResetObject(T &obj)
{
struct Container {
T member;
};
Container *const p = reinterpret_cast<Container*>(&obj);
p->~Container();
::new(p) Container();
}
The Standard has the following to say:
A pointer to a POD-struct object, suitably converted using a
reinterpret_cast, points to its initial
member (or if that member is a bit-field, then to the unit in which it
resides) and vice versa. [Note: There might therefore be unnamed
padding within a POD-struct object, but not at its beginning, as
necessary to achieve appropriate alignment. ]"
The above quote only applies to POD-struct objects. If my Container
class contains a non-POD member, then the Standard doesn't guarantee
that the address of a Container object is the same as the address of
its first member. However, given that my Container class only contains
1 member, it's quite likely that the two addresses will be equal.
The only way I can imagine my template function behaving unexpectedly
is if the implementation stores some sort of "hidden data" within the
Container type (perhaps some debugging information). Possible
scenarios are:
1) Padding or debugging information at the beginning of the Container
(which is permitted by the Standard if the Container Class is a non-
POD)
2) Padding or debugging information at the end of the Container (which
is permitted by the Standard for both POD's and non-POD's)
Anyway...
Anyone got any ideas on how to Reset any kind of object?
Any opinions?