Skip to content

Instantly share code, notes, and snippets.

View ipapadop's full-sized avatar
🖖

Yiannis Papadopoulos ipapadop

🖖
View GitHub Profile
@ipapadop
ipapadop / type_name.cpp
Created January 6, 2022 00:57
Get the name of a type
#include <string_view>
#include <iostream>
template<typename T>
constexpr std::string_view type_name()
{
constexpr const char prefix[] = "[with T = ";
constexpr std::string_view p = __PRETTY_FUNCTION__;
constexpr auto name_start = p.find(prefix) + sizeof(prefix) - 1;
constexpr auto name_end = p.find(';', name_start);
@ipapadop
ipapadop / detect_std.cpp
Created January 5, 2022 22:13
Detecting if a type is from STL
#include <string_view>
#include <ctre.hpp>
#include <cstdio>
#include <cstdint>
#include <vector>
namespace adv
{
template<class T>
constexpr std::string_view type_name()
@ipapadop
ipapadop / conjunction.cpp
Created May 4, 2021 19:43
std::conjunction and std::disjunction in C++11
// from https://www.fluentcpp.com/2021/04/30/how-to-implement-stdconjunction-and-stddisjunction-in-c11/
std::conjunction<Bs...>::value // is true if all Bs... are true, false otherwise
std::disjunction<Bs...>::value // is true if at least one of Bs... is true, false otherwise
// recursive
template<class...> struct conjunction : std::true_type { };
template<class B1> struct conjunction<B1> : B1 { };
@ipapadop
ipapadop / noexcept_call.cpp
Created October 14, 2020 19:17
C function is not marked noexcept, but you know it won't throw
// the C function is not marked noexcept, but you know it won't throw
// from https://twitter.com/hankadusikova/status/1276828584179642368
// technically UB
extern "C" int foo();
template <typename Fnc> struct noexcept_cast_helper;
template <typename Ret, typename... Args> struct noexcept_cast_helper<Ret(*)(Args...)> {
using type = Ret(*)(Args...) noexcept;
};
@ipapadop
ipapadop / poly_visit.cpp
Created September 30, 2020 18:27
Polymorphic visit
// https://quuxplusone.github.io/blog/2020/09/29/oop-visit/
#include <typeinfo>
#include <type_traits>
namespace my {
template<class T, class U> struct match_cvref { using type = U; };
template<class T, class U> struct match_cvref<T&, U> { using type = U&; };
template<class T, class U> struct match_cvref<T&&, U> { using type = U&&; };
template<class T, class U> struct match_cvref<const T, U> { using type = const U; };
@ipapadop
ipapadop / named_params.cpp
Created September 11, 2020 16:18
Named Parameters C++
// from https://pdimov.github.io/blog/2020/09/07/named-parameters-in-c20/
#include <memory>
#include <cstddef>
template<class T, class A = std::allocator<T>> class vector
{
private:
struct params
@ipapadop
ipapadop / fwd.cpp
Last active August 18, 2020 18:20
Proper forwarding
// see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0644r1.html
// C++14
template <class X, class Y>
decltype(auto) foo(X&& x, Y&& y) {
return std::forward<X>(x)(std::forward<Y>(y));
}
// C++14 (?)
@ipapadop
ipapadop / lift.cpp
Last active March 14, 2024 20:23
Passing overloaded functions to functions
// from https://blog.tartanllama.xyz/passing-overload-sets/
int foo (int);
float foo (float);
// Solution 1
#define FWD(...) std::forward<decltype(__VA_ARGS__)>(__VA_ARGS__)
#define LIFT(X) [](auto &&... args) \
@ipapadop
ipapadop / exploring_type.cpp
Created August 23, 2018 16:10
Exploring C++ types with puts(__PRETTY_FUNCTION__)
// from: https://quuxplusone.github.io/blog/2018/08/22/puts-pretty-function/
#include <stdio.h>
template<class T>
void f() {
#ifdef _MSC_VER
puts(__FUNCSIG__);
#else
puts(__PRETTY_FUNCTION__);
@ipapadop
ipapadop / rasbian_unattended_upgrades_gmail.markdown
Last active September 8, 2018 20:23 — forked from roydq/ubuntu_unattended_upgrades_gmail.markdown
Unattended upgrades on Rasbian Stretc with email notifications

Getting Started

Do yourself a favor and login as root to save yourself some time and headaches:

$ sudo su -

Install unattended-upgrades: