Skip to content

Instantly share code, notes, and snippets.

@Bktero
Bktero / options.txt
Created December 15, 2023 10:51
[C++][C] Warning options that are not in -Wall -Wextra
-Wmissing-include-dirs
Warn if a user-supplied include directory does not exist
@Bktero
Bktero / main.cpp
Created November 24, 2023 10:42
[C++20] Examples of the new time zone features in <chrono>
#include <chrono>
#include <iostream>
// Requires clang 17 or gcc 13
int main()
{
// UTC (system_clock is guaranteed to be UTC since C++20)
const auto now = std::chrono::system_clock::now();
std::cout << "UTC time = " << now << '\n';
@Bktero
Bktero / bititer.hpp
Last active November 24, 2023 10:41
[C++] Rust's BitIter in C++
// Inspired by https://lib.rs/crates/bit-iter
#include <algorithm>
#include <bitset>
#include <concepts>
#include <limits>
template <typename T>
requires std::unsigned_integral<T> class BitIter {
public:
@Bktero
Bktero / demangleTemplateType.hpp
Last active January 27, 2023 08:47
[C++] Helper function to get the demangled name of a type as a string (for gcc)
#pragma once
#include <string>
#include <cxxabi.h>
template<typename T>
std::string demangleTemplateType() {
const auto typeName = typeid(T).name();
const auto demangledName = abi::__cxa_demangle(typeName, nullptr, nullptr, nullptr);
const auto name = demangledName ? demangledName : typeName;
@Bktero
Bktero / enumerate.hpp
Created December 15, 2021 09:15
[C++14] Python's enumerate() in C++
#pragma once
#include <iterator>
// Inspired by / taken from:
// https://stackoverflow.com/questions/11328264/python-like-loop-enumeration-in-c
// https://www.reedbeta.com/blog/python-like-enumerate-in-cpp17/
template<typename Iterable>
auto enumerate(Iterable&& iterable) {
@Bktero
Bktero / extensions.md
Last active December 15, 2023 11:18
Settings and extensions for Visual Studio Code
@Bktero
Bktero / CMakeLists.txt
Last active March 3, 2021 09:10
[C++] Colored text in console with library Rang
cmake_minimum_required(VERSION 3.17)
project(hello_rang)
set(CMAKE_CXX_STANDARD 11)
# Get library from Github
include(FetchContent)
FetchContent_Declare(rang GIT_REPOSITORY https://github.com/agauniyal/rang.git GIT_TAG v3.1.0)
FetchContent_MakeAvailable(rang)
@Bktero
Bktero / print_color.py
Last active March 8, 2021 11:14
[Python] Colored text in console with library Colorama
import colorama
colorama.init(autoreset = True)
print(colorama.Back.GREEN + colorama.Fore.RED + 'hello, colored world')
@Bktero
Bktero / random.cpp
Last active March 8, 2021 11:28
[C++] Simple function to get random numbers in modern C++
#include <iostream>
#include <random>
template<typename T>
T random(T min = 0, T max = std::numeric_limits<T>::max()) {
static std::random_device randomDevice;
static std::mt19937 mersenneTwisterEngine(randomDevice());
std::uniform_int_distribution<T> distribution{min, max};
return distribution(mersenneTwisterEngine);
}
@Bktero
Bktero / trace.hpp
Last active November 25, 2020 11:09
[C++17] Recursive variadic template function with if constexpr to print a list of variables of any types to stdout | Equivalent function with fold expression
#include <iostream>
template<typename First, typename... Rest>
void trace(First&& first, Rest&& ... rest) {
if constexpr (sizeof...(rest) == 0) {
std::cout << std::forward<First>(first) << '\n';
} else {
std::cout << std::forward<First>(first) << ' ';
trace(std::forward<Rest>(rest)...);
}