Sind C ++ Nicht-Typ-Parameter (Funktion) Vorlagen bestellt?
-
02-07-2019 - |
Frage
Ich bin Hosting Spider in einem aktuellen Projekt und möchten Template-Funktionen haben einige der einfachen Eigenschaft get / set-Methoden erzeugen, zum Beispiel:
template <typename TClassImpl, int32 TClassImpl::*mem>
JSBool JS_DLL_CALLBACK WriteProp(JSContext* cx, JSObject* obj, jsval id, jsval* vp)
{
if (TClassImpl* pImpl = (TClassImpl*)::JS_GetInstancePrivate(cx, obj, &TClassImpl::s_JsClass, NULL))
return ::JS_ValueToInt32(cx, *vp, &(pImpl->*mem));
return JS_FALSE;
}
Gebraucht:
::JSPropertySpec Vec2::s_JsProps[] = {
{"x", 1, JSPROP_PERMANENT, &JsWrap::ReadProp<Vec2, &Vec2::x>, &JsWrap::WriteProp<Vec2, &Vec2::x>},
{"y", 2, JSPROP_PERMANENT, &JsWrap::ReadProp<Vec2, &Vec2::y>, &JsWrap::WriteProp<Vec2, &Vec2::y>},
{0}
};
Das funktioniert gut, aber wenn ich hinzufügen, ein anderes Mitglied Typ:
template <typename TClassImpl, JSObject* TClassImpl::*mem>
JSBool JS_DLL_CALLBACK WriteProp(JSContext* cx, JSObject* obj, jsval id, jsval* vp)
{
if (TClassImpl* pImpl = (TClassImpl*)::JS_GetInstancePrivate(cx, obj, &TClassImpl::s_JsClass, NULL))
return ::JS_ValueToObject(cx, *vp, &(pImpl->*mem));
return JS_FALSE;
}
Dann Visual C ++ 9 Versuche, die JSObject * Wrapper für int32 Mitglieder zu verwenden!
1>d:\projects\testing\jswnd\src\main.cpp(93) : error C2440: 'specialization' : cannot convert from 'int32 JsGlobal::Vec2::* ' to 'JSObject *JsGlobal::Vec2::* const '
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>d:\projects\testing\jswnd\src\main.cpp(93) : error C2973: 'JsWrap::ReadProp' : invalid template argument 'int32 JsGlobal::Vec2::* '
1> d:\projects\testing\jswnd\src\wrap_js.h(64) : see declaration of 'JsWrap::ReadProp'
1>d:\projects\testing\jswnd\src\main.cpp(93) : error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'JSPropertyOp'
1> None of the functions with this name in scope match the target type
Überraschenderweise parening JSObject * verursacht einen Parse-Fehler! . (Unerwartete '(') Dies ist wahrscheinlich ein Fehler VC ++ (kann jeder Test, dass "template void foo () {}" kompiliert in GCC) Gleicher Fehler mit „typedef JSObject * PObject;?. ..., PObject TClassImpl :: mem>“Leere , struct undefiniert * und Doppel Da die Funktionsnutzung vollständig instanziiert wird.‚& ReadProp‘, sollte es ins Spiel kommt keine normale Funktion Überlastung Semantik sein, ist es eine definierte Funktion an dass Punkt und erhält Vorrang vor Template-Funktionen. Es scheint, die Vorlage Bestellung hier versagt.
Vec2 ist nur:
class Vec2
{
public:
int32 x, y;
Vec2(JSContext* cx, JSObject* obj, uintN argc, jsval* argv);
static ::JSClass s_JsClass;
static ::JSPropertySpec s_JsProps[];
};
JSPropertySpec wird in JSAPI Link in OP beschrieben, von Kopf genommen:
typedef JSBool
(* JS_DLL_CALLBACK JSPropertyOp)(JSContext *cx, JSObject *obj, jsval id,
jsval *vp);
...
struct JSPropertySpec {
const char *name;
int8 tinyid;
uint8 flags;
JSPropertyOp getter;
JSPropertyOp setter;
};
Lösung
Ziemlich sicher, dass VC ++ hat "Probleme" hier. Comeau und g ++ 4.2 sind beide glücklich mit folgendem Programm:
struct X
{
int i;
void* p;
};
template<int X::*P>
void foo(X* t)
{
t->*P = 0;
}
template<void* X::*P>
void foo(X* t)
{
t->*P = 0;
}
int main()
{
X x;
foo<&X::i>(&x);
foo<&X::p>(&x);
}
VC ++ 2008sp1 ist jedoch nichts davon wissen.
Ich habe nicht die Zeit, um durch meinen Standard zu lesen, genau herauszufinden, was was ist ... aber ich denke, VC ++ im Unrecht ist hier.
Andere Tipps
Versuchen Sie, die JSObject * auf einem anderen Zeigertyp zu ändern, um zu sehen, ob das den Fehler wiedergibt. Ist JSObject an der Verwendungsstelle definiert? Auch vielleicht JSObject * muss in Pars sein.
Ich bin sicher kein Template-Guru, aber tue dieses einkochen zu einem subtilen Fall zu versuchen, Überlastungen rein auf dem Rückgabetyp basierten zu unterscheiden?
Da C ++ nicht Überlastung von Funktionen ermöglichen, basierend auf Rückgabetyp, vielleicht das Gleiche gilt für die Template-Parameter.