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
struct Si { | |
char tag; | |
int i; | |
}; | |
struct Sp { | |
char tag; | |
int* p; | |
}; | |
union U { | |
int i; |
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
template <typename T> // テンプレート共用体だって作れる | |
union U { | |
private: | |
struct { | |
bool is_allocated; | |
T i; | |
} si; | |
struct { | |
bool is_allocated; | |
T* p; |
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
template <typename T> | |
class BetterU { | |
bool is_allocated; | |
union { // 余分な構造体が必要ない | |
T i; | |
T* p; | |
}; | |
void Clean() { | |
if (IsAllocated()) { delete p; } | |
} |
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
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 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 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 <memory> | |
#include <type_traits> | |
template <class T, class... Args> | |
typename std::enable_if<std::is_constructible<T, Args...>::value, | |
std::unique_ptr<T> >::type | |
make_unique(Args&&... args) { | |
return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); | |
} | |
template <class T, class... Args> |
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
// template <class T, T... Ints> class integer_sequence; // in <utility> since C++14 | |
template <size_t... N> | |
struct IntegerSeq { | |
using type = IntegerSeq; | |
static constexpr size_t size() { return sizeof...(N); } | |
}; | |
template <typename S, typename T> | |
struct ConcatSeq_; |
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 <tuple> | |
#include <type_traits> | |
#include "integer_sequence.hpp" | |
template <typename T> | |
using resultof = typename std::result_of<T>::type; | |
template <typename F, typename... Args, size_t... I> | |
auto apply_(F&& f, std::tuple<Args...>&& args, IntegerSeq<I...>) | |
-> resultof<F(Args...)> { |
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 <type_traits> | |
// Usage: template <enabler_type<condition> = nullptr> | |
template <bool B> | |
using enabler_type = typename std::enable_if<B, std::nullptr_t>::type; |
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 <array> | |
#include <iostream> | |
#include <type_traits> | |
#define ENABLER(cond) \ | |
typename std::enable_if<(cond), std::nullptr_t>::type = nullptr | |
template <typename T, T... N> | |
struct IntegerSequence; |
OlderNewer