문제

내 호스팅 SpiderMonkey 에서는 현재 프로젝트하고 싶은 템플릿의 기능을 생성하는 몇 가지 간단한 숙박 시설/설정법,예를 들어:

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;
}

을 사용:

::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}
};

이 잘 작동하는 경우에,그러나 나는 다른 멤버를 추가 유형:

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;
}

Visual C++9 를 사용하려고 시도 JSObject*래퍼 int32 회원!

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

놀랍게도,parening JSObject*발생하는 오류!(예기치 않은'(').이것은 아마도 VC++러(수 있는 누군가 테스트는"템플릿 void foo(){}"에서 컴파일되는 GCC?).같은 오류"typedef JSObject*PObject;...,PObject TClassImpl::mem>",무효,구조체 정의되지 않은*,그리고 두 배.이 기능을 사용으로 인스턴스화:"&ReadProp",이 없어야 정상적인 기능을 하중 초과 의미를,그것은의 정의된 기능이 해당 지점에서와 우선 순위를 가져옵 템플릿능합니다.그것은 보인다는 템플 주문은 실패한 여기에.

Vec2 은:

class Vec2
{
public:
    int32 x, y;

    Vec2(JSContext* cx, JSObject* obj, uintN argc, jsval* argv);

    static ::JSClass s_JsClass;
    static ::JSPropertySpec s_JsProps[];
};

JSPropertySpec 에서 설명 JSAPI 링크에서 OP,헤더에서 가져온:

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;
};
도움이 되었습니까?

해결책

확 VC++는"문제"여기에.객 g++4.2 은 모두 행복으로 다음과 같은 프로그램:

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,그러나,그것은 전혀 필요하지 않습니다.

지 못하는 시간을 읽을 통해 내는 표준을 정확하게 무슨...그러나 내가 생각하는 VC++에 여기에 잘못.

다른 팁

변경하십시오 JSObject*다른 유형 포인터를 참조하는 경우에는 재생 오류가 있습니다.은 JSObject 에서 정의된 포인트의 사용합니까?또한,어쩌면 JSObject*해야에서 괄호.

나는 확실히 없는 템플릿 전문가지만,이 졸 미묘한 경우의 차별화를 위해 노력 오버로드를 기반으로 순수하게 반환에 입력?

이후 C++허용하지 않는 과부의 기능에 따라 반환 입력,아마도 동일한 것을 적용하여 템플릿을 매개 변수입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top