Created
February 6, 2022 13:49
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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