code_junkie

Are C++ non-type parameters to (function) templates ordered?

Nov 11th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. template <typename TClassImpl, int32 TClassImpl::*mem>
  2. JSBool JS_DLL_CALLBACK WriteProp(JSContext* cx, JSObject* obj, jsval id, jsval* vp)
  3. {
  4. if (TClassImpl* pImpl = (TClassImpl*)::JS_GetInstancePrivate(cx, obj, &TClassImpl::s_JsClass, NULL))
  5. return ::JS_ValueToInt32(cx, *vp, &(pImpl->*mem));
  6. return JS_FALSE;
  7. }
  8.  
  9. ::JSPropertySpec Vec2::s_JsProps[] = {
  10. {"x", 1, JSPROP_PERMANENT, &JsWrap::ReadProp<Vec2, &Vec2::x>, &JsWrap::WriteProp<Vec2, &Vec2::x>},
  11. {"y", 2, JSPROP_PERMANENT, &JsWrap::ReadProp<Vec2, &Vec2::y>, &JsWrap::WriteProp<Vec2, &Vec2::y>},
  12. {0}
  13. };
  14.  
  15. template <typename TClassImpl, JSObject* TClassImpl::*mem>
  16. JSBool JS_DLL_CALLBACK WriteProp(JSContext* cx, JSObject* obj, jsval id, jsval* vp)
  17. {
  18. if (TClassImpl* pImpl = (TClassImpl*)::JS_GetInstancePrivate(cx, obj, &TClassImpl::s_JsClass, NULL))
  19. return ::JS_ValueToObject(cx, *vp, &(pImpl->*mem));
  20. return JS_FALSE;
  21. }
  22.  
  23. 1>d:projectstestingjswndsrcmain.cpp(93) : error C2440: 'specialization' : cannot convert from 'int32 JsGlobal::Vec2::* ' to 'JSObject *JsGlobal::Vec2::* const '
  24. 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  25. 1>d:projectstestingjswndsrcmain.cpp(93) : error C2973: 'JsWrap::ReadProp' : invalid template argument 'int32 JsGlobal::Vec2::* '
  26. 1> d:projectstestingjswndsrcwrap_js.h(64) : see declaration of 'JsWrap::ReadProp'
  27. 1>d:projectstestingjswndsrcmain.cpp(93) : error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'JSPropertyOp'
  28. 1> None of the functions with this name in scope match the target type
  29.  
  30. class Vec2
  31. {
  32. public:
  33. int32 x, y;
  34.  
  35. Vec2(JSContext* cx, JSObject* obj, uintN argc, jsval* argv);
  36.  
  37. static ::JSClass s_JsClass;
  38. static ::JSPropertySpec s_JsProps[];
  39. };
  40.  
  41. typedef JSBool
  42. (* JS_DLL_CALLBACK JSPropertyOp)(JSContext *cx, JSObject *obj, jsval id,
  43. jsval *vp);
  44.  
  45. ...
  46.  
  47. struct JSPropertySpec {
  48. const char *name;
  49. int8 tinyid;
  50. uint8 flags;
  51. JSPropertyOp getter;
  52. JSPropertyOp setter;
  53. };
  54.  
  55. struct X
  56. {
  57. int i;
  58. void* p;
  59. };
  60.  
  61. template<int X::*P>
  62. void foo(X* t)
  63. {
  64. t->*P = 0;
  65. }
  66.  
  67. template<void* X::*P>
  68. void foo(X* t)
  69. {
  70. t->*P = 0;
  71. }
  72.  
  73. int main()
  74. {
  75. X x;
  76. foo<&X::i>(&x);
  77. foo<&X::p>(&x);
  78. }
Add Comment
Please, Sign In to add comment