Skip to content

Instantly share code, notes, and snippets.

@boatilus
Created October 19, 2016 02:35
Show Gist options
  • Save boatilus/e8f602be300b26fce0712fc42a88a72b to your computer and use it in GitHub Desktop.
Save boatilus/e8f602be300b26fce0712fc42a88a72b to your computer and use it in GitHub Desktop.
Basic SSO string
#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