TL; DR跳过“背景”部分,转到“问题”。
Background:我正在尝试实现一个模板类,该模板类封装了一个函数,该函数是另一个类的成员,该类的实例以及以后在调用该函数时提供的任何半恒定参数。回调。为此,我具有以下实现以及说明性的container
和main
实现:
template<class RET> class Callback_t {
public:
virtual ~Callback_t() = default;
virtual RET call() = 0;
};
template<class T, class RET, class... Args> class CallbackCreattimeArgs : public Callback_t<RET> {
public:
std::shared_ptr<T> owner;
RET(T::*x)(Args...);
RET call() {
return (*owner.*(x))(std::get<Args&&>(args)...);
};
std::tuple<Args&&...> args;
CallbackCreattimeArgs(std::shared_ptr<T> t, RET(T::*x)(Args...), Args&&... args) : owner(t), x(x),
args(std::tuple<Args&&...>(std::forward<Args>(args)...)) {}
CallbackCreattimeArgs(T* t, RET(T::*x)(Args...), Args&&... args) :
CallbackCreattimeArgs(std::shared_ptr<T>(t), x, std::forward<Args...>(args)...) {}
};
class container {
public:
static void printFrom(container* c) { c->print(); };
container(int data) : data(data) {};
~container() {};
void print() { printf("%d\n", data); };
void printTo(FILE* f) { fprintf(f, "%d\n", data); };
private:
int data;
};
int main() {
container c1(1), c2(20);
CallbackCreattimeArgs<container, void> f1(&c1, &container::print);
Callback_t<void>* fp1 = &f1;
fp1->call();//1
CallbackCreattimeArgs<container, void, FILE*> f2(&c2, &container::printTo, stdout);
Callback_t<void>* fp2 = &f2;
fp2->call();//20
}
这有效,但是不允许回调的调用者提供任何参数。我同样可以实现调用方提供的任意参数列表,但要以失去最初的创建者为代价。如果需要,我将发布此消息,但我认为它会增加更多的数量,然后变得更加清晰。
问题:已编辑,从此处完全重写:我需要支持Windows和Linux。我可以在Visual Studio 2017中使用它,但是linux g ++仍然无法编译。这是我当前的g ++版本:
#include <tuple>
#include <stdio.h>
template<class RET, class... RArgs> class Callback_t {
public:
virtual RET call(RArgs... rargs) = 0;
virtual ~Callback_t() = default;
};
template<class RET, class... RArgs> class CallbackFactory {
private:
template<class T, class... CArgs> class Callback : public Callback_t<RET, RArgs...> {
private:
T* owner;//TODO weak pointer?
RET(T::*x)(CArgs..., RArgs...);
std::tuple<CArgs...> cargs;
RET call(RArgs... rargs) {
return (*owner.*(x))(std::get<CArgs>(cargs)..., rargs...);
};
public:
Callback(T* t, RET(T::*x)(CArgs..., RArgs...), CArgs... pda);
~Callback() {};
};
public:
template<class U> Callback_t<RET, RArgs...>* make(U* owner, RET(U::*func)(RArgs...));
template<class U, class... CArgs> Callback_t<RET, RArgs...>* make(U* owner, RET(U::*func)(CArgs..., RArgs...), CArgs&... cargs);
};
template<class RET2, class... RArgs2> template<class T2, class... CArgs2> CallbackFactory<RET2, RArgs2...>::Callback<T2, CArgs2...>::Callback(T2* t, RET2(T2::*x)(CArgs2..., RArgs2...), CArgs2... pda) : x(x), owner(t), cargs(std::forward<CArgs2>(pda)...) {}
template<class RET, class... RArgs> template<class U> Callback_t<RET, RArgs...>* CallbackFactory<RET, RArgs...>::make(U* owner, RET(U::*func)(RArgs...)) {
return new CallbackFactory<RET, RArgs...>::Callback<U>(owner, func);
}
template<class RET, class... RArgs> template<class U, class... CArgs> Callback_t<RET, RArgs...>* CallbackFactory<RET, RArgs...>::make(U* owner, RET(U::*func)(CArgs..., RArgs...), CArgs&... cargs) {
return new CallbackFactory<RET, RArgs...>::Callback<U, CArgs...>(owner, func, std::forward<CArgs...>(cargs)...);
}
class container {
public:
static void printFrom(container* c) { c->print(); };
container(int data) : data(data) {};
~container() {};
void print() { printf("%d\n", data); };
void printTo(FILE* f) { fprintf(f, "%d\n", data); };
void printWith(int arg) { printf("%d:%d\n", data, arg); };
void printToWith(FILE* f, int arg) { fprintf(f, "%d:%d\n", data, arg); };
private:
int data;
};
int main() {
fprintf(stdout, "stdout test\n");
container c1(1), c2(20);
CallbackFactory<void, int> cbfi;
CallbackFactory<void> cbf;
Callback_t<void>* fp1 = cbf.make(&c1, &container::print);
fp1->call();//1
Callback_t<void>* fp2 = cbf.make(&c2, &container::printTo, stdout);
fp2->call();//20
Callback_t<void, int>* fp3 = cbfi.make(&c2, &container::printWith);
fp3->call(15);//20:15
Callback_t<void, int>* fp4 = cbfi.make<container, FILE*>(&c2, &container::printToWith, stdout);
fp4->call(200);//20:200
}
和编译器输出:
$ g++ test.cpp -o test.o -c -Wall -Wno-reorder -Wno-unused -std=c++14 -g
test.cpp: In function ‘int main()’:
test.cpp:60:96: error: no matching function for call to ‘CallbackFactory<void, int>::make(container*, void (container::*)(FILE*, int), _IO_FILE*&)’
Callback_t<void, int>* fp4 = cbfi.make<container, FILE*>(&c2, &container::printToWith, stdout);
^
test.cpp:25:48: note: candidate: template<class U> Callback_t<RET, RArgs ...>* CallbackFactory<RET, RArgs>::make(U*, RET (U::*)(RArgs ...)) [with U = U; RET = void; RArgs = {int}]
template<class U> Callback_t<RET, RArgs...>* make(U* owner, RET(U::*func)(RArgs...));
^
test.cpp:25:48: note: template argument deduction/substitution failed:
test.cpp:60:96: error: wrong number of template arguments (2, should be 1)
Callback_t<void, int>* fp4 = cbfi.make<container, FILE*>(&c2, &container::printToWith, stdout);
^
test.cpp:26:64: note: candidate: template<class U, class ... CArgs> Callback_t<RET, RArgs ...>* CallbackFactory<RET, RArgs>::make(U*, RET (U::*)(CArgs ..., RArgs ...), CArgs& ...) [with U = U; CArgs = {CArgs ...}; RET = void; RArgs = {int}]
template<class U, class... CArgs> Callback_t<RET, RArgs...>* make(U* owner, RET(U::*func)(CArgs..., RArgs...), CArgs&... cargs);
^
test.cpp:26:64: note: template argument deduction/substitution failed:
test.cpp:60:96: note: mismatched types ‘int’ and ‘FILE* {aka _IO_FILE*}’
Callback_t<void, int>* fp4 = cbfi.make<container, FILE*>(&c2, &container::printToWith, stdout);
因此g ++对于模板类型推导感到困惑,即使使用显式模板也是如此。另外,visualstudio / msbuild / windows希望在一个奇怪的地方使用typename关键字,这会杀死posix / gcc,所以我使用的是#ifdef _WIN32
。完整的可运行代码,其中包含更多测试用例,以供将来参考。我将继续称其为GPL beta版。
#ifdef _WIN32
#define wintypename typename
#else
#define wintypename
#endif
template<class RET, class... RArgs> class Callback_t {
public:
virtual RET call(RArgs... rargs) = 0;
virtual ~Callback_t() = default;
};
template<class RET, class... RArgs> class CallbackFactory {
private:
template<class T, class... CArgs> class Callback : public Callback_t<RET, RArgs...> {
private:
T * owner;//TODO weak pointer?
RET(T::*x)(CArgs..., RArgs...);
std::tuple<CArgs...> cargs;
RET call(RArgs... rargs) {
return (*owner.*(x))(std::get<CArgs>(cargs)..., rargs...);
};
public:
Callback(T* t, RET(T::*x)(CArgs..., RArgs...), CArgs... pda);
~Callback() {};
};
public:
template<class U, class... CArgs> static Callback_t<RET, RArgs...>* make(U* owner, CArgs... cargs, RET(U::*func)(CArgs..., RArgs...));
};
template<class RET2, class... RArgs2> template<class T2, class... CArgs2> CallbackFactory<RET2, RArgs2...>::Callback<T2, CArgs2...>::Callback(T2* t, RET2(T2::*x)(CArgs2..., RArgs2...), CArgs2... pda) : x(x), owner(t), cargs(std::forward<CArgs2>(pda)...) {}
template<class RET, class... RArgs> template<class U, class... CArgs> Callback_t<RET, RArgs...>* CallbackFactory<RET, RArgs...>::make(U* owner, CArgs... cargs, RET(U::*func)(CArgs..., RArgs...)) {
return new wintypename CallbackFactory<RET, RArgs...>::Callback<U, CArgs...>(owner, func, std::forward<CArgs>(cargs)...);
}
class container {
public:
static void printFrom(container* c) { c->print(); };
container(int data) : data(data) {};
~container() {};
void print() { printf("%d\n", data); };
void printTo(FILE* f) { fprintf(f, "%d\n", data); };
void printWith(int arg) { printf("%d:%d\n", data, arg); };
void printToWith(FILE* f, int arg) { fprintf(f, "%d:%d\n", data, arg); };
int print3AndSum(FILE* f, int a, int b) { fprintf(f, "%d:%d:%d\n", data, a, b); return a + b + data; };
private:
int data;
};
int main() {
fprintf(stdout, "stdout test\n");
container c1(1), c2(20);
Callback_t<void>* fp1 = CallbackFactory<void>::make(&c1, &container::print);
fp1->call();//1
Callback_t<void>* fp2 = CallbackFactory<void>::make<container, FILE*>(&c2, stdout, &container::printTo);
fp2->call();//20
Callback_t<void, int>* fp3 = CallbackFactory<void, int>::make(&c2, &container::printWith);
fp3->call(15);//20:15
Callback_t<void, int>* fp4 = CallbackFactory<void, int>::make<container, FILE*>(&c2, stdout, &container::printToWith);
fp4->call(200);//20:200
auto fp5 = CallbackFactory<void>::make<container, FILE*, int>(&c2, stdout, 70, &container::printToWith);
fp5->call();//20:70
auto fp6 = CallbackFactory<void, FILE*, int>::make(&c2, &container::printToWith);
fp6->call(stdout, 128);//20:128
auto fp7 = CallbackFactory<int, int>::make<container, FILE*, int>(&c2, stdout, 16, &container::print3AndSum);
auto i = fp7->call(32);//20:16:32
fp3->call(i);//20:68
}
感谢@Chipster的努力和建议,使我将函数定义移到了类之外。
[Edit:删除了不必要的基本模板