如果未知数组的大小,如何将函数数组作为参数传递

时间:2022-05-04 03:12:56

Here's a simple (and a silly one, yeah) example of what I'm trying to do:

这是一个简单的(也是一个愚蠢的,是的)我正在尝试做的例子:

#include <iostream>

void callFunctionsFromAnArray(void *(*)(int), int);
void f1(int);
void f2(int);
void f3(int);

int main()
{
    const int n = 3;
    void (*functions[n]) (int) = { f1, f2, f3 };
    callFunctionsFromAnArray(functions, n);
}

void callFunctionsFromAnArray(void *(*f) (int), int fCount) {
    for (int i = 0; i < fCount; i++)
        f[i](1);
}

void f1(int a)
{
    std::cout << a * 1 << '\n';
}
void f2(int a)
{
    std::cout << a * 2 << '\n';
}
void f3(int a)
{
    std::cout << a * 3 << '\n';
}

And those are the errors I'm getting when trying to compile:

这些是我在尝试编译时遇到的错误:

12:9: error: no matching function for call to 'callFunctionsFromAnArray'
        callFunctionsFromAnArray(functions, n);
3:10: note: candidate function not viable: no known conversion from 'void (*[3])(int)' to 'void *(*)(int)' for 1st argument
    void callFunctionsFromAnArray(void *(*)(int), int);
17:13: error: subscript of pointer to function type 'void *(int)'
            f[i](1);
2 errors generated.

However, if I change argument to be void (*f[3]) (int) it works. But the problem is, it's not always possible to know array's size before run-time (that's why function has 2nd argument after all). Is there any solution?

但是,如果我将参数更改为void(* f [3])(int)则可行。但问题是,在运行时之前并不总是能够知道数组的大小(这就是为什么函数毕竟有第二个参数)。有什么解决方案吗?

2 个解决方案

#1


2  

Since in this case, the array decays into a pointer, you don't have to know its size beforehand -- inside a function argument declaration, and only there, the pointer and the array qualifier (* and []) are equivalent, and the size of the array doesn't matter.

因为在这种情况下,数组衰减成指针,你不必事先知道它的大小 - 在函数参数声明中,只有那里,指针和数组限定符(*和[])是等价的,并且数组的大小无关紧要。

As a result, you can declare your function to take an array of 3 (or however many) pointers, and then you can pass it an array of any pointers:

因此,您可以声明您的函数采用3个(或多个)指针的数组,然后您可以传递任何指针的数组:

void callFunctionsFromAnArray(void *(*f[1])(int), int fCount)
{
    for (int i = 0; i < fCount; i++)
        f[i](i);
}

However, if you don't like this, you can just make it accept a pointer-to-pointer:

但是,如果你不喜欢这个,你可以让它接受一个指向指针的指针:

void callFunctionsFromAnArray(void *(**f)(int), int fCount); // etc.

However, if this really is C++, you'd be better off passing a const reference to a vector of std::functions:

但是,如果这真的是C ++,那么最好将const引用传递给std :: functions的向量:

void callFunctionsFromAnArray(const std::vector<std::function<void *(int)> > &a)
{
     // call'em
}

#2


2  

Since you are using C++ and not C, I highly suggest you to use std::vector and std::function instead. They will make your life much easier, especially in this particular case:

由于您使用的是C ++而不是C,我强烈建议您使用std :: vector和std :: function。它们将使您的生活更轻松,特别是在这种特殊情况下:

#include <functional>
#include <vector>

void f1(int);
void f2(int);
void f3(int);

using function_vector = std::vector<std::function<void(int)>>;

void callFunctions(const function_vector& vec) {
    for (const auto& f : vec)
        f(1);
}

int main() {
    function_vector vec = { &f1, &f2, &f3 };
    callFunctions(vec);
}

You can see a live example here.

你可以在这里看到一个实例。

#1


2  

Since in this case, the array decays into a pointer, you don't have to know its size beforehand -- inside a function argument declaration, and only there, the pointer and the array qualifier (* and []) are equivalent, and the size of the array doesn't matter.

因为在这种情况下,数组衰减成指针,你不必事先知道它的大小 - 在函数参数声明中,只有那里,指针和数组限定符(*和[])是等价的,并且数组的大小无关紧要。

As a result, you can declare your function to take an array of 3 (or however many) pointers, and then you can pass it an array of any pointers:

因此,您可以声明您的函数采用3个(或多个)指针的数组,然后您可以传递任何指针的数组:

void callFunctionsFromAnArray(void *(*f[1])(int), int fCount)
{
    for (int i = 0; i < fCount; i++)
        f[i](i);
}

However, if you don't like this, you can just make it accept a pointer-to-pointer:

但是,如果你不喜欢这个,你可以让它接受一个指向指针的指针:

void callFunctionsFromAnArray(void *(**f)(int), int fCount); // etc.

However, if this really is C++, you'd be better off passing a const reference to a vector of std::functions:

但是,如果这真的是C ++,那么最好将const引用传递给std :: functions的向量:

void callFunctionsFromAnArray(const std::vector<std::function<void *(int)> > &a)
{
     // call'em
}

#2


2  

Since you are using C++ and not C, I highly suggest you to use std::vector and std::function instead. They will make your life much easier, especially in this particular case:

由于您使用的是C ++而不是C,我强烈建议您使用std :: vector和std :: function。它们将使您的生活更轻松,特别是在这种特殊情况下:

#include <functional>
#include <vector>

void f1(int);
void f2(int);
void f3(int);

using function_vector = std::vector<std::function<void(int)>>;

void callFunctions(const function_vector& vec) {
    for (const auto& f : vec)
        f(1);
}

int main() {
    function_vector vec = { &f1, &f2, &f3 };
    callFunctions(vec);
}

You can see a live example here.

你可以在这里看到一个实例。