G
Gianni Mariani
David said:David said:[...]
Unless there is a defined conversion from int* to
MuthaObject*, I think it should elicit a diagnostic.
Maybe, but it is perfectly legal C++ (except for a typo I
just noticed).
I'm not sure what's so legal about it:
class MuthaObject { };
int main()
{
This was posted (typo fixed).
int* p = new int(42);
void* q = static_cast<void*>(p);
MuthaObject * r = static_cast<int*>(q);
delete r;
and you tried this ?
MuthaObject* p = new int;
}
"ComeauTest.c", line 5: error: a value of type "int *" cannot
be used to initialize an entity of type "MuthaObject *"
MuthaObject* p = new int;
I don't think you're testing what I posted.
Of course. What does that have to do with void*?
Maybe I mis-understood what you meant by
"the information must be saved somewhere".
What did you mean by that ?
Not sure what you're talking about here. I'm talking
about:
Specifically, the compiler MUST NOT apply a conversion from int * to
MuthaObject* (even if one exists) to be compliant to the standard with
the code snippet I posted.
MuthaObject * r = static_cast<int*>(q);
[...]
That's an interesting question. Theoretically, the
compiler *could* always keep track of what void* are
pointing to, but I'm not sure they are required to do so,
which is probably why it results in undefined behaviour.
I don't think you can. There are paths of data that are
outside the control of the conmpiler.
Such as?
Libraries, files, devices ...