简体   繁体   English

C++ 中简单可变参数 function 的不匹配模板?

[英]Unmatched template for simple variadic function in C++?

I am writing a Scheme to C++ compiler (although Scheme is not so related to this question), and I have a set of functions that are invoked in the Scheme code that can be used equivalently in the resulting C++ code.我正在为 C++ 编译器编写一个 Scheme(尽管 Scheme 与这个问题没有太大关系),并且我有一组在 Scheme 代码中调用的函数,可以在生成的 C++ 代码中等效使用。 I generated the C++ function "f" (seen below), and I cannot figure out for the life of me why it doesn't work (the error is below).我生成了 C++ function “f”(见下文),但我终其一生都无法弄清楚为什么它不起作用(错误如下)。 I have three templated parameters, and I provide the function with three arguments as well.我有三个模板化参数,我还提供了 function 和三个 arguments。 If you have any insight into this problem, please let me know - it's so incredibly frustrating!如果您对此问题有任何见解,请告诉我 - 这太令人沮丧了!

// from stdarg_macros.h
#include <stdarg.h>
#define INIT va_list args; va_start(args, nargs);
#define LOOP for (int i = 0; i < nargs; i++)
#define DEINIT va_end(args); return r;

// from io.cpp
#include <iostream>
template <typename T>
void display(T var) {
        std::cout << var << std::endl;
}

// from operators.c
int add(int nargs, ...) {INIT int r = 0; LOOP r += va_arg(args, int); DEINIT}
double add_d(int nargs, ...) { INIT double r = 0; LOOP r += va_arg(args, double); DEINIT}
int sub(int nargs, ...) {INIT int r; LOOP {if (i == 0) r = va_arg(args, int); else r -= va_arg(args, int);} DEINIT}
double sub_d(int nargs, ...) {INIT int r; LOOP {if (i == 0) r = va_arg(args, double); else r *= va_arg(args, double);} DEINIT}
int mul(int nargs, ...) {INIT int r = 1; LOOP r *= va_arg(args, int); DEINIT}
double mul_d(int nargs, ...) {INIT double r = 1; LOOP r *= va_arg(args, double); DEINIT}
double div_d(int nargs, ...) {INIT double r; LOOP {if (i == 0) r = va_arg(args, double); else r /= va_arg(args, double);} DEINIT}

// my transpiler's output file
template <typename T, typename S, typename M, typename x>
T f(S a, M b, x c) {
return mul_d(3, a, b, div_d(2, 3.0, c));
};

int main() {
        auto m = mul_d(3, 8.0, 2.0, div_d(2, 5.0, 3.0));
        display(m);
        display(f(1, 2, 3));
        return 0;
}
$ g++ -std=c++14 Output/math.cpp && ./a.out
    Output/math.cpp:9:9: error: no matching function for call to 'f'
    display(f(1, 2, 3));
        ^
    Output/math.cpp:3:3: note: candidate template ignored: couldn't infer template argument 'T'
    T f(g a, i b, L c) {
      ^
    1 error generated.

The template arguments could be deduced only from function arguments.模板 arguments 只能从 function arguments 推导出来。 In this case the 1st template parameter T of f can't be deduced , it's only used to specify function's return type.在这种情况下f的第一个模板参数T不能被推导,它仅用于指定函数的返回类型。

When possible, the compiler will deduce the missing template arguments from the function arguments.如果可能,编译器将从 function arguments 中推断出缺少的模板 arguments。

You have to specify the argument explicitly like您必须明确指定参数,例如

display(f<int>(1, 2, 3));
//       ^^^^^

Or you can remove the template parameter from f (and let the return type to be deduced):或者您可以从f中删除模板参数(并推导出返回类型):

template <typename S, typename M, typename x>
auto f(S a, M b, x c) {
    return mul_d(3, a, b, div_d(2, 3.0, c));
};

then you can just call it like那么你可以这样称呼它

display(f(1, 2, 3));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM