使用可变参数函数
我正在尝试使用 Visual Studio 2019 最新编译器在 C++ 中使用一些可变参数函数(尽管我的编译器可能有几个版本已经过时)。
我正在尝试编写一个函数来记录函数调用和有关它的一些信息,并返回函数调用的结果。
template <template <class, class> typename Function, class ...Args>
inline auto LogCall(std::string fun_name, Function<Args...> fun, std::string fun_param = "", Args... args) {
auto temp{ fun(args...) };
std::cout << fun_name << "of" << fun_param << "/t -> /t" << temp << '\n';
return temp;
}
起初,我只是使用 typename Function
但这不起作用,所以后来我尝试使用这个模板模板表单。无论哪种方式,当我调用它时,我都会收到有关没有重载版本匹配的错误。我当前正在尝试使用另一个模板函数来调用它,但我希望能够在任何函数上使用它,所以我想我可能需要两个,其中一个是此模板的某个版本,一个是此模板的某个版本,另一个不使用模板模板。
作为使用它的示例,我有一个简单的可变参数 max 函数,可以假设它可以工作并使用与此函数中所示相同的可变参数模板格式。这是调用的示例:
LogCall((string)"Max", max, (string)"1, 2, 3", 1, 2, 3);
我也尝试过传递模板参数,但它没有修复它:
LogCall((string)"Max", max<int, int, int>, (string)"1, 2, 3", 1, 2, 3);
对于此函数中的错误有什么建议吗?
I am trying to use some variadic functions in C++ using Visual Studio 2019 latest compiler (though my compiler may be a few versions out of date).
I am trying to write a function that logs out the function call and some information about it, and return the result of the function call.
template <template <class, class> typename Function, class ...Args>
inline auto LogCall(std::string fun_name, Function<Args...> fun, std::string fun_param = "", Args... args) {
auto temp{ fun(args...) };
std::cout << fun_name << "of" << fun_param << "/t -> /t" << temp << '\n';
return temp;
}
At first, I just used typename Function
but that didn't work, so then I tried using this template template form. Either way, I get an error about no overloaded version matches when I call it. I am trying to call it with another template function currently, but I want to be able to use this on any function, so I am thinking I may need two where one is some version of this template template one and one that does not use template template.
For an example of it being used I have a simple variadic max function that can be assumed works and uses the same variadic template format as seen in this function. Here is an example of the call:
LogCall((string)"Max", max, (string)"1, 2, 3", 1, 2, 3);
I have also tried passing the template arguments and it doesn't fix it:
LogCall((string)"Max", max<int, int, int>, (string)"1, 2, 3", 1, 2, 3);
Is there any suggestions as to what the error in this function is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论