声明为“静态内联”的函数的“const”“extern”函数ptr

问题描述

我只是想问问在 C 中使用“extern const”函数指针来暴露“静态内联”函数是否是一种好的风格。

示例:

lib.c:

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Request</title>
  <Meta charset="utf-8">
  <Meta name="viewport" content="width=device-width,initial-scale=1">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>

<body>

  <form target="_blank">
    <!-- Radio buttons for request type. Subsequent input fields will appear depending upon the request type -->
    <input type="radio" required name="type" id="ADD" class="reqType">
    <label class="form-check-label" for="ADD">Add new report</label>

    <input type="radio" name="type" id="DELETE" class="reqType">
    <label class="form-check-label" for="DELETE">Delete report</label>

    <!-- 'Add report' fields -->
    <div class="optionalInfo newRepData">
      <label for="reportId">New Report ID :</label>
      <input type="text" name="reportId" id="reportId">

      <label for="reportName">New Report Name :</label>
      <input type="text" name="reportName" id="reportName">
    </div>

    <br><br><input type="submit" value="Submit Request">

  </form>



</body>

</html>

lib.h

static inline int add(int x,int y) {return x+y;}

const int(*add_func)(int,int) = add;

main.c

extern const int(*add_func)(int x,int y);

我这样做的原因是:

  1. 我可以将函数声明为“内联”,而无需将这些函数放在头文件中并公开它们的定义,并且 LTO 应该负责在包含这些函数的地方内联这些函数

  2. 我可以将函数指针放在“const”结构中,并将其视为某种命名空间。

但我觉得这很混乱,如果 LTO 工作不正常,实际上可能会给函数调用增加一些开销。

那么问题是,我应该避免这种模式吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)