Skip to content

Instantly share code, notes, and snippets.

@hatsusato
hatsusato / union5.cpp
Last active January 1, 2016 03:59
snipet of union in C++11 for KMC Advent Calendar 2013
union Safe {
Safe(const char* src) : s{src} {}
~Safe() { s.~basic_string(); }
unsigned long long ull{0ULL}; // クラス内初期化は1つまで
std::string s;
};
union Danger {
Danger(const char* src) : s{src} {}
~Danger() { s.~basic_string(); }
std::string s;
@hatsusato
hatsusato / union4.cpp
Last active January 1, 2016 03:29
snipet of union in C++11 for KMC Advent Calendar 2013
class U {
enum class Tag { String, Vector } tag;
union {
std::string s;
std::vector<int> v;
};
void Clean() {
switch (tag) {
case Tag::String:
s.~basic_string();
@hatsusato
hatsusato / union3.cpp
Last active January 1, 2016 03:29
snipet of union in C++03 for KMC Advent Calendar 2013
template <typename T>
class BetterU {
bool is_allocated;
union { // 余分な構造体が必要ない
T i;
T* p;
};
void Clean() {
if (IsAllocated()) { delete p; }
}
@hatsusato
hatsusato / union2.cpp
Last active January 1, 2016 03:29
snipet of union in C++03 for KMC Advent Calendar 2013
template <typename T> // テンプレート共用体だって作れる
union U {
private:
struct {
bool is_allocated;
T i;
} si;
struct {
bool is_allocated;
T* p;
@hatsusato
hatsusato / union1.c
Created December 22, 2013 18:07
snipet of union in C for KMC Advent Calendar 2013
struct Si {
char tag;
int i;
};
struct Sp {
char tag;
int* p;
};
union U {
int i;