Skip to content

Instantly share code, notes, and snippets.

View DieHertz's full-sized avatar

Andrey Mironov DieHertz

  • Tel-Aviv
View GitHub Profile
#include <iostream>
#include <type_traits>
using namespace std;
class NullType;
template<size_t, template<class> class...> struct nth_type {
template<class V> using type = NullType;
};
#include <cstdlib>
class const_str {
const char* str;
size_t len;
public:
template<size_t N> constexpr const_str(const char (&str)[N]) : str{str}, len{N - 1} {}
constexpr size_t size() const { return len; }
};
@DieHertz
DieHertz / any.h
Last active August 9, 2017 11:45
#ifndef any_h
#define any_h
namespace utility {
class any {
struct impl_base {
virtual impl_base* clone() const = 0;
virtual ~impl_base() = default;
};
#include <iostream>
#include <typeinfo>
#include <cstdint>
struct bit_field {
uint32_t a : 6;
};
struct helper {
union {
#include <iostream>
namespace ex {
template<class T> void foo(T) { std::cout << "primary template\n"; }
}
template<> void ex::foo<int>(int) { std::cout << "specialization outside of namespace\n"; }
#include <iostream>
int main() {
unsigned a = 1;
float b = 2.0f;
auto c = -a * b;
std::cout << c << std::endl;
}
#include <iostream>
struct null_ptr_t {
constexpr null_ptr_t() {}
template<typename T> operator T*() const { return nullptr; }
};
constexpr null_ptr_t null_ptr;
template<class T1, class T2> struct some_template {
using true_ = char(&)[1];
using false_ = char(&)[2];
struct host {
operator T1*() const;
operator T2*();
};
template<typename T>
#include <iostream>
int main() {
auto a = int{};
const auto b = sizeof(++a) == sizeof(int);
std::cout << a << ' ' << b << '\n';
}
#include <iostream>
int foo(int& a) { return ++a; }
int main() {
auto a = int{};
const auto b = sizeof(foo(a)) == sizeof(int);
std::cout << a << ' ' << b << '\n';
}