Skip to content

Instantly share code, notes, and snippets.

@jdiego
jdiego / bytes_to_array.cpp
Created August 7, 2018 00:37
Convert size_t (4 or 8) bytes to array of bytes same length.
#include <array>
#include <cstddef>
constexpr auto dissolve(size_t value) noexcept {
return [=]<size_t... I>(std::index_sequence<I...>) noexcept -> std::array<uint8_t, sizeof(size_t)>{
return { static_cast<uint8_t>((value >> (((sizeof(size_t)-1)*8) - (8 * I)) ) & 0xFF)... };
}(std::make_index_sequence<sizeof(size_t)>());
}
auto convert2(size_t v) {
@jdiego
jdiego / print_type.cpp
Created August 25, 2018 00:39
Printing C++ Types with __PRETTY__FUNCTION__. You can use this C++ trick to explore what the compiler is doing to your types, either for metaprogramming purposes or just to learn more about auto, auto&&, and decltype(auto)
#include <cstdio>
template<typename T>
void f(void)
{
puts(__PRETTY_FUNCTION__);
}
#define EXPLORE(expr) \
printf("decltype(" #expr ") is ..."); \
f<decltype(expr)>();
@jdiego
jdiego / client_with_check.py
Created September 22, 2018 19:36
An HTTP client that checks that the content of the response has at least Content-Length bytes.
#!/usr/bin/env python3
#
# An HTTP client that checks that the content of the response has at least
# Content-Length bytes.
#
# For requests 2.x (should not be needed for requests 3.x).
#
import requests
import sys
@jdiego
jdiego / enumerate.hpp
Last active November 25, 2018 22:58
Python-Like enumerate() In C++17
#include <tuple>
/*
Example of use:
int main(void)
{
std::vector<std::string> things = {"Hello", "World", "!"};
for (auto [i, thing] : enumerate(things))
{
@jdiego
jdiego / effective_modern_cmake.md
Last active July 13, 2019 02:18 — forked from mbinna/effective_modern_cmake.md
Effective Modern CMake

Effective Modern CMake

Getting Started

For a brief user-level introduction to CMake, watch C++ Weekly, Episode 78, Intro to CMake by Jason Turner. LLVM’s CMake Primer provides a good high-level introduction to the CMake syntax. Go read it now.

After that, watch Mathieu Ropert’s CppCon 2017 talk Using Modern CMake Patterns to Enforce a Good Modular Design (slides). It provides a thorough explanation of what modern CMake is and why it is so much better than “old school” CMake. The modular design ideas in this talk are based on the book [Large-Scale C++ Software Design](https://www.amazon.de/Large-Scale-Soft

@jdiego
jdiego / safe_advance.h
Created September 26, 2019 02:38 — forked from oliora/safe_advance.h
Implementation of safe std::advance, std::next and std::prev similar to proposed N4317 paper "New Safer Functions to Advance Iterators"
// N4317 "New Safer Functions to Advance Iterators" paper can be found at
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4317.pdf
namespace detail {
template<class InputIter>
inline void do_advance(InputIter& it, InputIter end, typename std::iterator_traits<InputIter>::difference_type n, std::input_iterator_tag)
{
assert(n >= 0);
for (; (n > 0) && (it != end); --n)
++it;
#include <memory>
#include <unordered_map>
#include <typeindex>
#include <iostream>
std::unordered_map<std::type_index, std::shared_ptr<void>(*)(void const*)> cloners;
template <typename Derived>
struct cloneable
{
@jdiego
jdiego / source_location.cpp
Created October 17, 2019 11:35
How to use source_location in a variadic template function?
#include <iostream>
#include <utility>
#include <experimental/source_location>
/*
The C++20 feature std::source_location is used to capture information about the context in which a function is called.
*/
template <typename... Ts>
struct debug
{