公共库的 char 或 std::string 数组?

发布于 2024-08-13 13:28:53 字数 384 浏览 2 评论 0原文

我的问题很简单:
我应该使用 char 数组,例如:

char *buf, buf2[MAX_STRING_LENGTH]  

等,还是应该在其他程序员使用的库中使用 std::string ,他们可以在他们选择的任何 SO 和编译器上使用它?

考虑到性能和可移植性...

从我的角度来看,std 字符串更容易并且性能相同,或者差异太小而不能不使用 std:string,关于可移植性我不知道。我想因为它是标准的,所以不应该有任何编译器在没有它的情况下编译 C++,至少不应该有任何重要的编译器。

编辑:
该库将在 3 个主要操作系统上编译,理论上,作为库分发

您的想法?

蒂,

my question is simple:
Should I use array of char eg:

char *buf, buf2[MAX_STRING_LENGTH]  

etc or should I use std::string in a library that will be used by other programmers where they can use it on any SO and compiler of their choice?

Considering performance and portability...

from my point of view, std strings are easier and performance is equal or the difference is way too little to not use std:string, about portability I don't know. I guess as it is standard, there shouldn't be any compiler that compiles C++ without it, at least any important compiler.

EDIT:
The library will be compiled on 3 major OS and, theorically, distributed as a lib

Your thoughts?

ty,
Joe

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

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

发布评论

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

评论(5

别忘他 2024-08-20 13:28:53

取决于该库如何与客户端代码结合使用。如果它将被动态链接,并且您有一组为客户端公开的 API,那么您最好使用以 null 结尾的字节字符串(即 char *)及其宽字符对应项。如果您正在讨论在代码中使用它们,那么您当然可以自由使用 std::string。如果它将包含在源代码形式中 - std::string 工作正常。

Depends on how this library will be used in conjunction with client code. If it will be linked in dynamically and you have a set of APIs exposed for the client -- you are better off using null terminated byte strings (i.e. char *) and their wide-character counterparts. If you are talking about using them within your code, you certainly are free to use std::string. If it is going to be included in source form -- std::string works fine.

ぃ弥猫深巷。 2024-08-20 13:28:53

但是,如果您的库作为 DLL 提供,您的用户将必须使用 std::string 的相同实现。如果您的库是使用 Microsoft STL 构建的,他们将无法使用 STLPort(或任何其他实现)。

But if your library is shipped as DLL your users will have to use the same implementation of std::string. It won't be possible for them to use STLPort (or any other implementation) if your library was built using Microsoft STL.

孤独陪着我 2024-08-20 13:28:53

只要您的库的目标是纯 C++,使用 std::string 就很好,甚至是可取的。但是,这样做会将您与 C++ 的特定实现(用于构建库的实现)联系起来,并且它不能与其他 C++ 实现或其他语言链接。

通常,非常希望为库提供 C 接口而不是 C++ 接口。这样,任何其他提供 C 外部函数接口的语言(这是其中的大多数)都可以使用它。对于C接口,需要使用char *

As long as you are targetting pure C++ for your library, using std::string is fine and even desirable. However, doing that ties you to a particular implementation of C++ (the one used to build your library), and it can't be linked with other C++ implementations or other languages.

Often, it is highly desirable to give a library a C interface rather than a C++ one. That way its usable by any other language that provides a C foreign function interface (which is most of them). For a C interface, you need to use char *

冰雪之触 2024-08-20 13:28:53

我建议只使用 std::string。此外,如果您希望与需要 C 风格字符串的库(例如,使用 C 兼容 API)兼容,您始终可以使用 std::string 的 c_str() 方法。

I would recommend just using std::string. Besides if you want compatibility with libraries requiring C-style strings (for example, which uses a C compatible API), you can always just use the c_str() method of std::string.

已下线请稍等 2024-08-20 13:28:53

一般来说,使用 std::string 会更好,尤其是对于库内部的调用。

对于您的 API,它取决于其用途。对于组织内的内部使用,使用 std::string 的 API 可能就可以了。对于外部使用,您可能希望提供一个 C API,其中使用 char*

In general, you will be better off using std::string, certainly for calls internal to your library.

For your API, it's dependent on what its purpose is. For internal use within your organization, an API that uses std::string will probably be fine. For external use you may wish to provide a C API, one which uses char*

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