Search code examples
c++stringcharstandards

Array of char or std::string for a public library?


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?


Solution

  • 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.