函数参数中的 struct 关键字以及 const 正确性

发布于 2024-11-18 06:48:35 字数 536 浏览 7 评论 0原文

我的库中有一个不透明类型定义为:

typedef struct MyOpaqueType* MyType;  // easier to type for client code

我无法使用 typedef 传递指向 const 结构的指针,因此某些函数看起来像:

void UsePointerToConst ( const struct MyOpaqueType * )

而不是:

void UserPointerToConst( const MyType ) // can't use, is really constant pointer

因此,考虑到这一点,我有两个问题: 参数列表中的 struct 关键字是否只在 C 中才需要? 有更好的方法吗?我应该创建一个 typedef 例如:

typedef const struct MyOpaqueType* ConstantMyType; ?

I have an opaque type in my library defined as:

typedef struct MyOpaqueType* MyType;  // easier to type for client code

I can't pass a pointer-to-const struct around using the typedef, so some functions look like:

void UsePointerToConst ( const struct MyOpaqueType * )

instead of:

void UserPointerToConst( const MyType ) // can't use, is really constant pointer

So, given this, I have two questions:
Is the struct keyword in the parameter list only necessary in C?
Is there a better way to do this? Should I create a typedef such as:

typedef const struct MyOpaqueType* ConstantMyType; ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稳稳的幸福 2024-11-25 06:48:35

参数列表中的struct关键字是不是只有C语言才需要?

是的。请参阅 Jens Gustedt 的回答。

有更好的方法吗?

只是 typedef 结构体,而不是指针。这样做更好,因为

  • 您只需要一个 typedef,而不是每个 {MyOpaqueTypeMyOpaqueType *MyOpaqueType const *、MyOpaqueType *constMyOpaqueType const *const} 以及涉及 restrict 的所有变体(不存在于C++),
  • 用户很清楚指针语义适用,即传递数据类型实际上是指针复制的问题(无需担心性能),用户不太可能忘记使用后进行清理,C++用户可以使用智能指针,这
  • 是一个常见的 C 约定(想想 FILE *)。

也没有危险;当有人忘记 * 时,他们会收到编译器错误。

Is the struct keyword in the parameter list only necessary in C?

Yes. See Jens Gustedt's answer.

Is there a better way to do this?

Just typedef the struct, not the pointer. This is better because

  • you only need one typedef instead of one for each of {MyOpaqueType, MyOpaqueType *, MyOpaqueType const *, MyOpaqueType *const and MyOpaqueType const *const} and all variants involving restrict (which doesn't exist in C++),
  • it's clear to the user that pointer semantics apply, i.e., passing the datatype around is really a matter of pointer copying (no performance worries), users are less likely to forget cleaning up after use, C++ users may use smart pointers, and
  • it's a common C convention (think FILE *).

There's also no danger; when someone forgets the *, they get a compiler error.

薄荷梦 2024-11-25 06:48:35

在 C++ 中,只要没有其他标识符具有该名称,就假定 typedefstruct 同名。因此,类似接收 struct stat* 作为参数的函数 stat 之类的东西:

int stat(const char *path, struct stat *buf);

在 C++ 中甚至是允许的。 (这是一个真实世界的示例。)

因此,您最好使用前向声明,例如

typedef struct toto toto;

在标识符和结构名称空间中保留标记 toto 。然后您可以为 C 和 C++ 声明函数接口。但如果您也想从 C 访问它,请不要忘记 extern "C"

另请参阅:这个答案结构标签不是 C++ 中的标识符

In C++ a typedef of the same name as a struct is assumed as long as there is no other identifier with that name. So something like a function stat that receives a struct stat* as an argument:

int stat(const char *path, struct stat *buf);

is even allowed in C++. (This is a real world example.)

So you are always better of with forward declarations like

typedef struct toto toto;

which reserves the token toto in the identifier and in the struct name space. Then you can have your function interface declared for C and C++. But don't forget the extern "C" if you want to access it from C, too.

See also: this answer on SO and struct tags are not identifiers in C++.

静谧 2024-11-25 06:48:35

在 C++ 中根本不需要 typedef。只需使用前向声明:

struct MyType;

然后根据需要传递 MyType const *MyType *MyType const & 等。

You don't need the typedef at all in C++. Just use a forward declaration:

struct MyType;

Then pass around MyType const *, MyType *, MyType const & etc as and when required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文