[英]template class no instance constructor
I want to be able to make my Wrapper shorter.我希望能够使我的 Wrapper 更短。
I dont want to have to include decltype(foo)
twice in each line我不想在每行中包含两次decltype(foo)
What is the right way to use the constructor to be able yo call _foo2 like im trying.使用构造函数能够像我尝试的那样调用 _foo2 的正确方法是什么。
class Helper {
public:
Helper(LPCTSTR filename) : _filename(filename) {}
~Helper() { }
template<typename T>
T CallFunction() {
return T();
}
private:
LPCTSTR _filename;
};
template<typename T> class WrapFuncObj;
template<typename T, typename... Args>
class WrapFuncObj<T(Args...)> {
public:
WrapFuncObj(Helper* dll, LPCTSTR func) : _dll(dll), _func(func) {}
T operator()(Args&&... args) {
return _dll->CallFunction<T>();
}
private:
Helper* _dll;
LPCTSTR _func;
};
class Export {
Helper _dll{ L"SayHello.dll" };
public:
WrapFuncObj<decltype(foo)> _foo = WrapFuncObj<decltype(foo)>(&_dll, L"foo"); // works
WrapFuncObj<decltype(foo)> _foo2 = WrapFuncObj(&_dll, L"foo"); // problem here
};
You can provide the constructor arguments directly, no need to indirect through another WrapFuncObj
functional cast.您可以直接提供构造函数 arguments,无需通过另一个WrapFuncObj
函数转换来间接提供。 It works the same way as you used it for _dll
:它的工作方式与您用于_dll
的方式相同:
WrapFuncObj<decltype(foo)> _foo2{&_dll, L"foo"};
Just be aware that initialization with parentheses is not allowed for default initializers of data members, you need to use braces.请注意,数据成员的默认初始化程序不允许使用括号进行初始化,您需要使用大括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.