Skip to content

Instantly share code, notes, and snippets.

template <class T>
T Deserialize(const void* memory) noexcept {
static_assert(std::is_trivially_copyable_v<T>);
T t;
memcpy(std::addressof(t), memory, sizeof(T));
return t;
}
// std::remove_reference_t for non-deduced context to prevent such code to blow below:
// char first = f(); char second = g();
#include <type_traits>
template<class T1, class T2>
struct MyPair {
T1 first;
T2 second;
static constexpr bool has_references = std::is_reference_v<T1> ||
std::is_reference_v<T2>;
MyPair(const T1& x, const T2& y) : first(x), second(y) {}
template <class T1, class T2, bool is_reference>
struct MyPairBase;
// Dispatch the reference specialisation and use it as a storage.
template <class T1, class T2>
struct MyPairBase<T1, T2, false> {
T1 a;
T2 b;
MyPairBase& operator=(const MyPairBase& other) = default;
};
template <typename T, typename U>
struct pair {
T first;
U second;
};
int main() {
int x = 10;
int y = 20;
pair<int&, int> p{x, x};
struct __nat {};
pair& operator=(typename conditional<
is_copy_assignable<first_type>::value &&
is_copy_assignable<second_type>::value,
pair, __nat>::type const& __p)
noexcept(is_nothrow_copy_assignable<first_type>::value &&
s_nothrow_copy_assignable<second_type>::value)
{
first = __p.first;
template <class T>
T Deserialize(const void* memory) {
T t;
memcpy(&t, memory, sizeof(T));
return t;
}
template <class T>
void Serialize(const T& t, void* memory) {
memcpy(memory, &t, sizeof(T));
template <typename T, typename U>
struct pair {
T first;
U second;
constexpr std::strong_ordering operator<=>(const pair& other) const {
if (auto first_comp = first <=> other.first; first_comp != 0) {
return first_comp;
} else {
return second <=> other.second;
constexpr std::pair<int, int> t;
static_assert(t.first == 0 && t.second == 0);
std::array<int, 5> arr;
// assert(arr[0] == 0); // Undefined behavior
@danlark1
danlark1 / pair_easy.h
Created April 13, 2020 10:55
iteration
std::unordered_map<std::string, int> m;
// ...
for (const auto& [key, value] : m) {
// ...
}
size_t CalcScore(const Problem& p, const std::vector<size_t>& order) {
MPSolver solver("solving_all_cases",
MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING);
std::vector<MPVariable*> books_vars;
std::vector<std::vector<MPVariable*>> libraries_vars;
const double infinity = solver.infinity();
for (size_t i = 0; i < p.books_num; ++i) {
books_vars.push_back(solver.MakeBoolVar(absl::StrCat("x_", i)));
}
for (const size_t ind : order) {