detrazione C ++ Template durante la chiamata una funzione template
-
26-10-2019 - |
Domanda
FastDelegate
riferisce a http://www.codeproject.com/KB/cpp/ FastDelegate.aspx , ma io non penso che è in relazione.
Ho codice come seguire, e l'errore ottenuto.
#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'
}
Se ha cambiato il main()
a:
int main()
{
FastDelegate0<int> fdGetter(Getter<int>);
FastDelegate1<int> fdSetter(Setter<int>);
Prop<int>* c = MakeProp(fdGetter, fdSetter); // It works.
}
o
int main()
{
FastDelegate0<int> fdGetter(Getter<int>);
Prop<int>* c = MakeProp<int>(fdGetter, Setter<int>); // It works, too.
}
Credo che, MakeProp()
dovrebbe ottenere il T
da fdGetter (che è int
, che ha chiamato il contructor di FastDelegate1<int>
automaticamente. Ma non l'ha fatto. Perché?
P.S. Vorrei salvare il getter e setter in Prop
, ogni suggerimento per questo approccio è il benvenuto. Forse è un male per copiare l'istanza di FastDelegate * durante il passaggio di argomenti nella funzione.
Soluzione
Hai provato
Prop<int>* c = MakeProp(FastDelegate0<int>(Getter<int>), FastDelegate1<int>(Setter<int>));
Setter<int>
non può essere convertito in FastDelegate1<T>
!
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow