Skip to content

Instantly share code, notes, and snippets.

View DieHertz's full-sized avatar

Andrey Mironov DieHertz

  • Tel-Aviv
View GitHub Profile
@DieHertz
DieHertz / WhatIsStrictAliasingAndWhyDoWeCare.md
Created December 28, 2023 12:11 — forked from shafik/WhatIsStrictAliasingAndWhyDoWeCare.md
What is Strict Aliasing and Why do we Care?

What is the Strict Aliasing Rule and Why do we care?

(OR Type Punning, Undefined Behavior and Alignment, Oh My!)

What is strict aliasing? First we will describe what is aliasing and then we can learn what being strict about it means.

In C and C++ aliasing has to do with what expression types we are allowed to access stored values through. In both C and C++ the standard specifies which expression types are allowed to alias which types. The compiler and optimizer are allowed to assume we follow the aliasing rules strictly, hence the term strict aliasing rule. If we attempt to access a value using a type not allowed it is classified as undefined behavior(UB). Once we have undefined behavior all bets are off, the results of our program are no longer reliable.

Unfortunately with strict aliasing violations, we will often obtain the results we expect, leaving the possibility the a future version of a compiler with a new optimization will break code we th

#include <map>
#include <iostream>
struct range : std::pair<int, int> {
using std::pair<int, int>::pair;
void foo() const {
left = 10;
}
#include <ext/map.hpp>
#include <vector>
#include <iostream>
int main() {
const std::vector<std::pair<std::string, std::string>> collection{
{ "first", "primero" },
{ "second", "segundo" },
{ "third", "tercero" }
};
#include <chrono>
#include <random>
#include <iostream>
template<typename U> void match(const U&) {}
template<typename U, typename Expr> void match(const U&, const Expr& expr) { expr(); }
template<typename U, typename Expr, typename... Args>
void match(const U& u, const U& cond, Expr expr, const Args&... args) {
return u == cond ? expr() : match(u, args...);
}
#include <chrono>
#include <random>
#include <iostream>
template<typename T> struct match_impl {
template<typename F> match_impl& operator()(const T& cond, F f) {
if (value == cond) f();
return *this;
}
#include <stack>
#include <map>
#include <vector>
#include <iostream>
#include <cstdint>
using symbol = char;
using state = std::uint32_t;
using symbol_state_pair = std::pair<symbol, state>;
using transition_map = std::map<symbol_state_pair, std::vector<state>>;
@DieHertz
DieHertz / lambda-expression-capture-const.cpp
Last active August 29, 2015 14:06
There seems to be a bug in g++ 4.8.2, 4.9.0 and probably further releases.
struct foo {
void bar() {}
void baz() const {
[this] { bar(); }();
}
};
#include <type_traits>
#include <iostream>
template<typename... T> struct type_list;
template<typename T, typename List> struct in;
template<typename T, typename Head, typename... Tail> struct in<T, type_list<Head, Tail...>> {
static constexpr auto value = std::is_same<T, Head>::value || in<T, type_list<Tail...>>::value;
};
template<typename T> struct in<T, type_list<>> {
#include <type_traits>
#include <utility>
template<typename R, typename... Args>
struct function_ptr_wrapper {
using type = R(*)(Args...);
type function;
function_ptr_wrapper(type function) : function{function} {}
R operator()(Args... args) const { return (*function)(args...); }
@DieHertz
DieHertz / range.hpp
Last active August 29, 2015 14:05
Android created Gist
template<typename T> struct range_iterator {
T pos;
T& operator*() { return pos; }
range_iterator& operator++() { return ++pos, *this; }
bool operator!=(const range_iterator& other) { return pos != other.pos; }
};