Skip to content

Instantly share code, notes, and snippets.

@Bueddl
Last active September 25, 2016 21:56
Show Gist options
  • Save Bueddl/627949b5376c25c28a2ce2d51d8a8be2 to your computer and use it in GitHub Desktop.
Save Bueddl/627949b5376c25c28a2ce2d51d8a8be2 to your computer and use it in GitHub Desktop.
#include <assert.h>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <type_traits>
template<std::size_t N>
class label
{
public:
label() = default;
void set_text(const char *str)
{
std::size_t size = std::strlen(str);
assert(size < N);
std::copy_n(str, size, static_cast<char*>(m_str));
}
const char* get_text() const
{
return m_str;
}
std::size_t get_max_size() const
{
return N;
}
private:
char m_str[N];
};
template<std::size_t N, typename... Ts>
struct label<N> make_label(const char (&str)[N], Ts&&... args)
{
label<N> label{std::forward<Ts>(args)...};
label.set_text(str);
return label;
}
int main()
{
auto lbl = make_label("My Label");
std::cout << "length of label '" << lbl.get_text()
<< "': " << lbl.get_max_size() << std::endl;
lbl.set_text("Very, very long label");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment