Created
October 19, 2016 02:35
-
-
Save boatilus/e8f602be300b26fce0712fc42a88a72b to your computer and use it in GitHub Desktop.
Basic SSO string
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 <cassert> | |
#include <cstddef> | |
#include <string> | |
template <int8_t len> | |
class string { | |
private: | |
char arr[len + 1]; | |
public: | |
string() { arr[len - 1] = len; } | |
uint8_t size() const noexcept { return static_cast<uint8_t>(len - arr[len - 1]); } | |
void operator=(const std::string& s) { | |
// no check for overflow | |
for (std::size_t i { 0 }; i < s.size(); i++) { | |
arr[i] = s[i]; | |
} | |
arr[len - 1] = len - s.size(); | |
} | |
}; | |
int main() { | |
string<3> s; | |
s = "a"; | |
assert(s.size() == 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment