Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic types to expose on a C++ API

I'm targeting Windows but I don't see any reason why some API code I'm writing cannot use basic C++ types. What I want to do is expose methods that return strings and ints. In the C# world I'd just use string, and have a unicode string, but in VC++ I've got the option of using std::string, std::wstring, or MFC/ATL CStrings.

Should I just use std::wstring exclusively to support unicode, or can I use std::string which would be compiled to unicode based on my build settings? I'm leaning toward the latter. I'd prefer to provide Get[Item]AsCString() methods on my objects for other string types.

Also should I be using size_t instead of integer?

The API is going to be used by me, and perhaps a future developer working on the C++ GUI. It is a way to separate concerns. My preferences:

  • Intuitiveness for other developers.
  • Forward compatibility with VC++
  • Compatibility with other C++ compilers
  • Performance (this is a lesser concern for me, but need the startup time for rest of my app)

Any guides would be appreciated.

like image 701
WTLNewbie Avatar asked Feb 27 '11 19:02

WTLNewbie


1 Answers

You should probably stick to the STL string type. The MFC CString class is built on top of that nowadays anyway.

As has been noted before, using wstring is not a magic bullet to address Unicode issues since there are many Unicode characters that still require multiple wchars to encode.

Using Utf-8 instead has potential benefits (you don't have to worry about endianness for example).

On Windows, all modern kernels are wchar based, so there is a (minimal) performance overhead involved if you use the 8bit char versions of APIs.

like image 69
COrthbandt Avatar answered Nov 15 '22 10:11

COrthbandt