Search code examples
qtc-standard-library

Reason to use Qt standard library function wrappers


Is there any reason to use Qt standard function wrappers like qstrncpy instead of strncpy?

I could not find any hint in documentation. And I'm curious if there is any functional difference. It looks like making code dependent on Qt, even in not mandatory places.

I found this: Qt wrapper for C libraries But it doesn't answer my question.


Solution

  • These methods are part of Qt's efforts for platform-independence. Qt tries to hide platform differences and use the best each platform has to offer, replicating that functionality on platforms where it is not available. Here is what the documentation of qstrncpy has to say:

    A safe strncpy() function.

    Copies at most len bytes from src (stopping at len or the terminating '\0' whichever comes first) into dst and returns a pointer to dst. Guarantees that dst is '\0'-terminated. If src or dst is nullptr, returns nullptr immediately.

    […]

    Note: When compiling with Visual C++ compiler version 14.00 (Visual C++ 2005) or later, internally the function strncpy_s will be used.

    So qstrncpy is safer than strncpy.