微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决声明为“静态内联”的函数的“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 举报,一经查实,本站将立刻删除。