1

FastDelegate指的是http://www.codeproject.com/KB/cpp/FastDelegate.aspx,但我不认为它是相关的。

我有如下代码,但出现错误。

#include <FastDelegate.h>


using namespace fastdelegate;

template <typename T>
T Getter() {}

template <typename T>
void Setter(T) {}

template <typename T>
class Prop
{
public:
    typedef FastDelegate0<T> Getter;
    typedef FastDelegate1<T> Setter;

    Prop(Getter getter, Setter setter) :
        m_Getter(getter), m_Setter(setter)
    {

    }

private:
    Getter m_Getter;
    Setter m_Setter;
};

template <typename T>
inline Prop<T>* MakeProp(FastDelegate0<T> getter, FastDelegate1<T> setter)
{
    return new Prop<T>(getter, setter);
}

static int Target = 0;
int main()
{
    FastDelegate0<int> fdGetter(Getter<int>);
    Prop<int>* c = MakeProp(fdGetter, Setter<int>);
    // ^^^^ error: no matching function for call to 'MakeProp'
}

如果更改main()为:

int main()
{
    FastDelegate0<int> fdGetter(Getter<int>);
    FastDelegate1<int> fdSetter(Setter<int>);
    Prop<int>* c = MakeProp(fdGetter, fdSetter); // It works.
}

或者:

int main()
{
    FastDelegate0<int> fdGetter(Getter<int>);
    Prop<int>* c = MakeProp<int>(fdGetter, Setter<int>); // It works, too.
}

我认为,MakeProp()应该得到Tfrom fdGetter (即int,比FastDelegate1<int>自动调用的构造函数。但它没有。为什么?

PS 我想将 getter 和 setter 保存在 中Prop,欢迎对这种方法提出任何建议。在函数中传递参数时复制 FastDelegate* 的实例可能是不好的。

4

1 回答 1

0

你有没有尝试过

Prop<int>* c = MakeProp(FastDelegate0<int>(Getter<int>), FastDelegate1<int>(Setter<int>));

?

Setter<int>无法转换为FastDelegate1<T>

于 2011-10-18T17:29:30.460 回答