John Doe said:
This compiles just fine in VC++7.1 ...
But is not currently *guaranteed* to work ( => not legal C++ ).
Container implementations would be allowed, for example,
to store a root element within the class itself,
or may otherwise use the sizeof() of the provided
parameter to select a data member or specialization,
or whatever...
So the code is not (formally) portable, even though
it might work with all your compilers in practice.
You mean that they are doing particular cases in the compiler like Java
is doing for Strings? That would be really bad. I hate that: a language
should be just a language and should treat all data types equally.
I did not mean that this would only apply to std::vector.
The fact is: the code above works well with several implementations.
But the standard allows standard library implementations to break it
(e.g. it could be needed to allow some optimizations on some platforms).
So declaring the above code above legal requires a cost/benefit analysis,
and maybe it cannot apply to all containers (e.g. std::string,
which often stores a small buffer as a data member, would not work).
But anyway I don't think it's the reason for which it compiles. I tried
with my own template instead of std::string and it compiled on the same,
because the std::string basically just uses pointers to the classes, so
it just requires a forward definition of such classes in order to
compile correctly, right?
Right - in your implementation.
This was GREAT code, unfortunately it does not compile.
It compiles as is on Comeau++, which is considered by many as being
a reference in compliance (
http://www.comeaucomputing.com/tryitout/).
What is the exact error message you are getting?
I guess it is because in the implementation of f you are putting a
pointer to a global function, while the definition of FP requires a
__thiscall function pointer (a function member of a class, in which
"this" is passed secretely). I'm NOT sure though.
The sample above does not involve any member functions, just
a data member that is of type pointer-to-function.
Is there any cast which can make the above code compile?
I tried but I wasn't able to do it.
Please post the exact code you tried and associated error message.
The snippet is 'kosher' as far as I can tell (although it is not
an example of clarity...).
Cheers,
Ivan