使用可变参数函数

发布于 2025-01-15 16:44:47 字数 1035 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文