简体   繁体   English

如何指定一个 C++ 指针指向一个 C function 与未知数量的 ZDBC11CAA5BDA99F77E6FBDED47

[英]How to specify a C++ pointer to a C function with unknown number of arguments?

I'm writing a C library which I would like to be usable from both C and C++.我正在编写一个 C 库,我希望它可以从 C 和 C++ 中使用。 At one moment, it should take a callback from the user with 0-3 arguments, which will be called at some pointer later.在某一时刻,它应该使用 0-3 arguments 接收用户的回调,稍后将在某个指针处调用该回调。 Like this (a copy of the code is available as GitHub Gist too):像这样(代码的副本也可以作为GitHub Gist获得):

// app_c.c
#include <stdio.h>
#include "lib.h"

double f0(void) {
    return 123;
}

double f2(double a, double b) {
    return a + b;
}

int main() {
    cb_arity = 0;
    cb_func = f0;
    printf("%f\n", cb_call());

    cb_arity = 2;
    cb_func = f2;
    printf("%f\n", cb_call());
}

I was able to create a pointer to a C function which takes unknown (but still fixed) number of arguments, note it's void (*cb_func)() , not void (*cb_func)(void) :我能够创建一个指向 C function 的指针,它需要未知(但仍然固定)数量的 arguments,注意它是void (*cb_func)() void (*cb_func)(void)

// lib.h
#ifndef LIB_H_
#define LIB_H_

#ifdef __cplusplus
extern "C" {
#endif

extern int cb_arity;
extern double (*cb_func)();
double cb_call(void);

#ifdef __cplusplus
}
#endif

#endif  // LIB_H_
// lib.c
#include "lib.h"
#include <stdlib.h>

int cb_arity;
double (*cb_func)();

double cb_call(void) {
    switch (cb_arity) {
        case 0:
            return cb_func();
        case 1:
            return cb_func(10.0);
        case 2:
            return cb_func(10.0, 20.0);
        case 3:
            return cb_func(10.0, 20.0, 30.0);
        default:
            abort();
    }
}

It compiles and runs successfully both on my machine and Wandbox .它可以在我的机器和Wandbox上成功编译和运行。 As far as I understand, no UB is invoked.据我了解,没有调用 UB。

Now I would like to make it work in C++ as well.现在我想让它也可以在 C++ 中工作。 Unfortunately, it looks like I now need reinterpret_cast because () means "no arguments" in C++, not "unknown number of arguments":不幸的是,看起来我现在需要reinterpret_cast因为()在 C++ 中表示“无参数”,而不是“未知数量的参数”:

// app_cpp.cpp
#include <stdio.h>
#include "lib.h"

int main() {
    cb_arity = 0;
    cb_func = []() { return 123.0; };
    printf("%f\n", cb_call());

    cb_arity = 2;
    cb_func = reinterpret_cast<double(*)()>(static_cast<double(*)(double, double)>(
        [](double a, double b) { return a + b; }
    ));
    printf("%f\n", cb_call());
}

As far as I understand, no UB is invoked here as well: although I convert function pointer double(*)(double, double) to double(*)(void) in C++, it's converted back to double(*)(double, double) in C code right before calling.据我了解,这里也没有调用 UB:虽然我在 C++ 中将 function 指针double(*)(double, double)转换为double(*)(void) ,但它已转换回double(*)(double, double)在调用之前在 C 代码中。

Is there any way to get rid of these ugly casts in C++ code?有什么办法可以消除 C++ 代码中这些丑陋的演员表? I've tried specifying type of cb_func as void(*)(...) , but C++ still won't implicitly convert double(*)(double, double) to it.我尝试将cb_func的类型指定为void(*)(...) ,但 C++ 仍然不会隐式地将double(*)(double, double)转换为它。

Rather than erase the number of arguments from the callback, you could retain it.您可以保留它,而不是从回调中删除 arguments 的数量。

// lib.h
#ifndef LIB_H_
#define LIB_H_

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    int arity;
    union {
        void(*zero)(void);
        void(*one)(double);
        void(*two)(double, double);
        void(*three)(double, double, double);
    }
} cb_type;
extern cb_type cb;

double cb_call(void);

#ifdef __cplusplus
}
#endif

#endif  // LIB_H_
// lib.c
#include "lib.h"
#include <stdlib.h>

cb_type cb;

double cb_call(void) {
    switch (cb.arity) {
        case 0:
            return cb.zero();
        case 1:
            return cb.one(10.0);
        case 2:
            return cb.two(10.0, 20.0);
        case 3:
            return cb.three(10.0, 20.0, 30.0);
        default:
            abort();
    }
}

If you don't expose cb , you can't mismatch the arity and union member:如果您不公开cb ,则不能不匹配 arity 和 union 成员:

// lib.h
#ifndef LIB_H_
#define LIB_H_

#ifdef __cplusplus
extern "C" {
#endif

void register_zero(void(*)(void));
void register_one(void(*)(double));
void register_two(void(*)(double, double));
void register_three(void(*)(double, double, double));

double cb_call(void);

#ifdef __cplusplus
}
#endif

#endif  // LIB_H_

Parameterless function declarations is an obsolescent C feature.无参数 function 声明是过时的 C 功能。 I would suggest avoiding it.我建议避免它。

Essentially, you're trying to circumvent type checking: you want to store an almost arbitrary function in a cb_func variable, and then be able to call it freely without any signs of danger like explicit casts.本质上,您是在尝试规避类型检查:您希望将几乎任意的 function 存储在cb_func变量中,然后能够在没有任何危险迹象(如显式强制转换)的情况下自由调用它。 However, this code is inherently dangerous: if you mess up cb_arity , behavior is undefined.然而,这段代码本质上是危险的:如果你搞砸了cb_arity ,行为是不确定的。 Moreover, you can even store double(*)(int, char*) to cb_func without any warnings, which is never ok in your example.此外,您甚至可以在没有任何警告的情况下将double(*)(int, char*)存储到cb_func ,这在您的示例中是绝对不行的。

On a deeper level, your cb_arity / cb_func looks a lot like a tagged union .在更深层次上,您的cb_arity / cb_func看起来很像一个带标签的 union The canonical way to implement it in C++ is std::variant (or something function-specific like unique_pseudofunction ).在 C++ 中实现它的规范方法是std::variant (或特定于函数的东西,如unique_pseudofunction )。 The canonical way to implement it in C is a struct with a "tag" field and an anonymous union , like this:在 C 中实现它的规范方法是具有“标签”字段和匿名union的结构,如下所示:

struct cb_s {
    int arity;
    union {
        double (*func0)(void);
        double (*func1)(double);
        double (*func2)(double, double);
        double (*func3)(double, double, double);
    };
};

extern struct cb_s cb;

Now instead of cb_func = f0 you write cb.func0 = f0 , and instead of cb_func = f2 you write cb.func2 = f2 .现在你写 cb_func = f0 而不是cb_func = f0 cb.func0 = f0 ,而不是cb_func = f2你写cb.func2 = f2 Similary in C++ and all casts from lambdas are now gone. C++ 和所有来自 lambda 的演员现在都消失了。 The only remaining sign of danger is the underlying union .唯一剩下的危险迹象是潜在的union

You will have to change your code in two places:您将不得不在两个地方更改您的代码:

  1. The library.图书馆。 Here you already know which of func0 / func1 /... to call, not a big deal.在这里,您已经知道要调用哪个func0 / func1 /...,没什么大不了的。
  2. The user code which wrote to cb_func .写入cb_func的用户代码。 Now it has to know which union member to write into.现在它必须知道要写入哪个工会成员。 Presumably, the same code would also write a compile-time constant cb_arity , so not a big deal agian.据推测,相同的代码也会编写一个编译时常cb_arity ,所以没什么大不了的。 If cb_arity is received from someone else, that someone should then also use tagged union instead of passing cb_arity and cb_func separately for better type safety.如果从其他人那里收到cb_arity ,那么该人也应该使用标记联合,而不是分别传递cb_aritycb_func以获得更好的类型安全性。

Sidenote: C++ (as opposed to C) prohibits access to a non-active member of union.旁注:C++(相对于 C)禁止访问非活动的工会成员。 This should not affect correctness of the code, because calling a func0 through a pointer to func1 is UB by itself.这不应该影响代码的正确性,因为通过指向func1的指针调用func0本身就是 UB。

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

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