Skip to content

Instantly share code, notes, and snippets.

@voidbar
Created February 6, 2022 13:49
Show Gist options
  • Save voidbar/7b133f055201b86a9283f75773682c33 to your computer and use it in GitHub Desktop.
Save voidbar/7b133f055201b86a9283f75773682c33 to your computer and use it in GitHub Desktop.
A small utility function to get the null terminator of some string type. Can be useful when dealing with systems where sizeof(wchar_t) can change
#include <string>
#include <iostream>
#include <string>
template <typename T>
constexpr auto NullTerminator() {
return std::string(sizeof(typename T::value_type), '\0');
}
int main() {
std::cout << "Null terminator length of std::string: " << NullTerminator<std::string>().size() << "\n";
// This will return 2 on windows and 4 on linux/mac
std::cout << "Null terminator length of std::wstring: " << NullTerminator<std::wstring>().size() << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment