Skip to content

Instantly share code, notes, and snippets.

View nthery's full-sized avatar

Nicolas Thery nthery

  • Grenoble - France
View GitHub Profile
@nthery
nthery / forward_vs_move_full.cpp
Last active May 23, 2022 07:20
value categories, reference flavors, std::move() vs std::forward()
// value categories, reference flavors, std::move() vs std::forward()
#include <string>
#include <vector>
struct Bar {
int *payload_;
Bar();
~Bar();
@nthery
nthery / word_counter.cpp
Created May 7, 2022 16:11
Word counter implemented with transform_reduce()
// Word counter implemented with transform_reduce()
// Stolen from https://www.youtube.com/watch?v=LW_T2RGXego&t=571s
#include <iterator>
#include <functional>
#include <cctype>
#include <iostream>
#include <numeric>
#include <execution>
// Templated constructor in derived class.
#include <utility>
struct B {
B();
B(int);
B(const B&);
@nthery
nthery / updating_map_value.cpp
Created February 2, 2022 09:27
This program shows various ways of inserting and updating values in maps.
// This program shows various ways of inserting and updating values in maps.
#include <unordered_map>
#include <string>
#include <cassert>
int main() {
std::unordered_map<std::string, int> m;
m.emplace("foo", 1);
@nthery
nthery / name_lookup_mystery.md
Last active January 13, 2022 09:22
A C++ name lookup mystery

A C++ name lookup mystery

Alternate title: yet another reason to loathe C++.

The offending code

// foo.hpp

namespace root::foo {
@nthery
nthery / CMakeLists.txt
Created December 21, 2021 09:29
cmake gtest skeleton
cmake_minimum_required(VERSION 3.11)
project(dummy_test_project)
set(CMAKE_CXX_STANDARD 17)
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
@nthery
nthery / boost_serialization_blob.cpp
Created December 8, 2021 10:12
Show how to serialize object to binary blob and restore it using Boost.Serialization
#include <sstream>
#include <iostream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
struct Foo {
int the_answer = 0;
double pi = 0.0;
@nthery
nthery / use_after_free_with_non_local_shared_pointer.cpp
Created December 7, 2021 07:59
Example of use-after-free caused by non-local shared pointer being reseated while underlying raw pointer in use.
// Example of use-after-free caused by non-local shared pointer being reseated
// while underlying raw pointer in use.
// https://godbolt.org/z/jTrnzejf7
#include <memory>
#include <iostream>
std::shared_ptr<int> g_sp = std::make_shared<int>(42);
void g() {
@nthery
nthery / check_higher_order_parameters.cpp
Last active November 29, 2021 09:15
How to check that callable passed to higher-order function has expected prototype
// How to check that callable passed to higher-order function has
// expected prototype in C++17 (pending C++20 concepts).
#include <type_traits>
#include <functional>
// Check with static_assert that F can be invoked with something convertible to int
// and returns something convertible to double.
template<class F>
double call(F f) {
@nthery
nthery / move_vs_copy_when_resizing_vector.cpp
Last active November 26, 2021 15:58
Micro-benchmark showing impact of throwing move constructors when resizing vector
// Benchmark showing the impact of throwable move ctors when resizing a vector.
#include <vector>
#include <algorithm>
#include <iostream>
#include <benchmark/benchmark.h>
constexpr int PAYLOAD_SIZE = 1024;