c++ - 如何在 C++ 中模板化接受模板化参数并在其上应用模板化函数的函数?

标签 c++ templates function-pointers alias pointer-to-member

我有一堆静态类函数,它们接受不同数量的 {string, int, float} 参数和一个输出参数。根据所调用的函数,相同的参数可能会有不同的行为。例如:

static void ChangeOutput1(const string& foo, int bar, Output* output);
static void ChangeOutput2(int bar, Output* output);
static void ChangeOutput3(float foo, Output* output);
static void ChangeOutput4(float foo, Output* output);  // behaves differently from ChangeOutput3

我想要一种简单、安全的方式来编写模板以对每个函数执行类似的行为,基本上是使用 Output 参数调用并将其转换为字符串以返回。理想情况下无需再次指定参数类型。它可能看起来像这样:

template<typename... Args, int (*ChangeOutputFn)(Args...)>
string OutputString(Args... args) {
    Output output;
    ChangeOutputFn(args..., &output);
    return ConvertToString(output);
}

// Is there a way to alias static templated functions?
using StringOutput1 = OutputString<ChangeOutput1>;
using StringOutput2 = OutputString<ChangeOutput2>;
using StringOutput3 = OutputString<ChangeOutput3>;

我不确定如何实现这一目标。我不确定如何编写 OutputString 以及我将如何别名或定义静态函数。有不太优雅的解决方案,但它们需要重复的样板代码,我想避免。

最佳答案

对于一个类,你可以做这样的事情:

template <typename T, T f> struct OutputString;

template<typename... Args, void (*ChangeOutputFn)(Args...)>
struct OutputString<void (*)(Args...), ChangeOutputFn>
{
    template <typename ... Ts>
    auto operator()(Ts... args)
    -> decltype(ChangeOutputFn(std::forward<Ts>(args)..., std::declval<Output *>()),
                std::string{})
    {
        Output output;
        ChangeOutputFn(std::forward<Ts>(args)..., &output);
        return ConvertToString(output);
    }
};

然后

using StringOutput1 = OutputString<decltype(&ChangeOutput1), &ChangeOutput1>;
using StringOutput2 = OutputString<decltype(&ChangeOutput2), &ChangeOutput2>;
using StringOutput3 = OutputString<decltype(&ChangeOutput3), &ChangeOutput3>;

并将其用作

std::string s2 = StringOutput2{}(42);
std::string s3 = StringOutput3{}(4.2f);

Demo

关于c++ - 如何在 C++ 中模板化接受模板化参数并在其上应用模板化函数的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33729013/

相关文章:

c++ - 如何使用 cv::parallel_for_ 减少执行时间

c++ - 不使用 "this"处理当前窗口?

css - 如何从 Blogger 购物模板中删除页脚文本

c++ - 从基类继承私有(private)成员

C++函数指针问题

C++ 指针算术循环访问冲突

c++ - 如何获取字符串的所有 1 位或相邻的 2 位数字组合

c++ - 数独检查器 - 如何检查子网格中的重复项

php - 使用 PHP STDIN 和 proc_open 以及 Node JS 命令

c# - 在转换为 "out/ref"的表达式中支持 "object"参数