Skip to content

Instantly share code, notes, and snippets.

@talybin
talybin / is_var_fn.hpp
Last active June 10, 2024 22:36
Attempt to detect if function has variadic number of arguments
#include <utility>
struct any_type
{
template <class T>
constexpr operator T(); // non explicit
};
namespace detail
{
@talybin
talybin / recursive_visitor.cpp
Last active June 9, 2024 23:42
Recursive visitor
#include <utility>
#include <cstddef>
#include <stdexcept>
#include <ctime>
struct test_t { };
namespace detail
{
template <size_t I>
@talybin
talybin / packed_tuple.hpp
Last active September 24, 2021 07:40
A tuple that can be aligned to any number (not just power of 2) and does not use "pragma pack"
#pragma once
#include <type_traits>
#include <tuple>
#include <memory>
template <std::size_t N, class... Types>
struct packed_tuple;
// is_packed_tuple
template <class T>
@talybin
talybin / constexpr_counter.hpp
Last active September 19, 2021 20:08
Constant expression counter
namespace detail
{
template <class T>
struct flag
{
struct dummy {
constexpr dummy() {}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnon-template-friend"
@talybin
talybin / to_const_integral.cpp
Last active August 25, 2021 21:18
Yet another way to convert run-time integer to constant.
#include <iostream>
#include <ctime>
#include <utility>
template <class T, class F, T... I>
bool to_const(T value, F&& fn, std::integer_sequence<T, I...>) {
return (
(value == I && (fn(std::integral_constant<T, I>{}), true))
|| ... // or continue
);
@talybin
talybin / concept_lite.cpp
Created August 20, 2021 19:30
Concept Lite (Filip Roséen)
#include <type_traits>
template <class T, class = std::enable_if_t<std::is_integral<T>::value>>
using integral_t = T;
// the usage
template <class T>
void f(integral_t<T> a) {}
int main()
@talybin
talybin / type_only_function.cpp
Last active August 20, 2021 09:52
Type only function (no object required)
#include <iostream>
struct printer
{
template <class T>
constexpr void operator()(const T& t) const {
std::cout << t << '\n';
}
};
@talybin
talybin / one_arg_function.cpp
Last active August 12, 2021 07:45
Function taking any argument
#include <iostream>
#include <functional>
#include <any>
struct any_type : std::any
{
using std::any::any;
template <class T> operator T() const {
return std::any_cast<T>(*this);
}
@talybin
talybin / type_name.hpp
Last active August 18, 2021 09:37
Type name revisited. This generates only actual type of name in output binary.
#pragma once
#include <utility>
template <class T, std::size_t... I>
inline const char* type_name(std::index_sequence<I...>) {
static constexpr char name[] = { __PRETTY_FUNCTION__[I + 60]..., 0 };
return name;
}
template <class T>
@talybin
talybin / inner_struct.cpp
Last active August 5, 2021 23:48
The type of inner struct.
#include <iostream>
#include <string_view>
template <class T>
constexpr std::string_view type_name(const T&)
{
std::string_view sv = __PRETTY_FUNCTION__;
sv.remove_prefix(sv.find('=') + 2);
return { sv.data(), sv.find(';') };