This file contains hidden or 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
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; |
This file contains hidden or 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
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(); |
This file contains hidden or 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
template <typename T> | |
class BetterU { | |
bool is_allocated; | |
union { // 余分な構造体が必要ない | |
T i; | |
T* p; | |
}; | |
void Clean() { | |
if (IsAllocated()) { delete p; } | |
} |
This file contains hidden or 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
template <typename T> // テンプレート共用体だって作れる | |
union U { | |
private: | |
struct { | |
bool is_allocated; | |
T i; | |
} si; | |
struct { | |
bool is_allocated; | |
T* p; |
This file contains hidden or 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
struct Si { | |
char tag; | |
int i; | |
}; | |
struct Sp { | |
char tag; | |
int* p; | |
}; | |
union U { | |
int i; |
NewerOlder