Re: template static function pointer error
On 18 Feb., 06:21, "wes" <wesley.h...@gmail.com> wrote:
I'm getting a mysterious error using static funtion pointers in a
templated C++ class.
Let me give you an introductory advice: If you would like to
ask the group for some help like this, your code (snippet)
should be as selfdescribing as possible. The information you
provide is very incomplete, v.i.
The essentials of the class look like:
template <class T>
class udata
{
typedef void (*fptr)(T);
public:
protected:
static fptr kill_func;
};
And the specializations look like:
template <class T>
typename udata<T>::fptr udata<T>::kill_func = free;
1) This is not a specialization, its the definition of
a static class member of the primary class template.
2) I expect a leading #include <stdlib.h> here.
3) Even if this necessary #include is provided, it is
implementation-defined, whether the code up to this
point will successfully compile or not. The reason is,
that your fptr typedef implies C++ linkage, but the
Standard does not guarantee that functions from the
C library do have either C or C++ linkage see 17.4.2.2/2:
"Whether a name from the Standard C library declared
with external linkage has extern "C" or extern "C++"
linkage is implementation-defined.[..]"
template <dWorldID>
typename udata<dWorldID>::fptr udata<dWorldID>::kill_func =
dWorldDestroy;
1) What does dWorldID mean here?
2) This is also no valid specialization.
I guess you wanted to explicitely specializy the data member
for pointers on dxWorld (Note that your primary seems to
imply that it should only be instantiated for *pointers*,
because the expected argument of the fptr typedef is T
(and not T*). Here I fill some of the gaps:
class dxWorld;
void dWorldDestroy(dxWorld*);
template <> // Explicit specialization for dxWorld*
udata<dxWorld*>::fptr udata<dxWorld*>::kill_func =
dWorldDestroy;
This does compile and a similar specialization for
some dxSpace* does also.
template <dSpaceID>
typename udata<dSpaceID>::fptr udata<dSpaceID>::kill_func =
dSpaceDestroy;
When I compule with GCC 4.0.1, I get the error:
lua_ode_udata.h:68: error: invalid conversion from 'void (*)
(dxSpace*)' to 'void (*)(dxWorld*)'
This is really baffling because dWorldDestroy has the signature void
(*)(dxWorld*) which is not what the error indicates. I noticed that
when I swapped the declarations, the error reversed itself. It seems
that somehow the last specialization for the static member kill_func
is overwritting the previous one. Has anyone ever seen this before?
There are a lot of unknowns here, because you did not explain
what dSpaceID and dWorldID should mean. Your specialization syntax
is also incorrect.
Furtheron I would recommend that you redesign your original template
in a way, that it makes sense to instantiate it with non-pointer
types, something like the following:
template <class T>
class udata
{
typedef void (*fptr)(T*);
protected:
static fptr kill_func;
};
Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]