Skip to content

Instantly share code, notes, and snippets.

View louis-langholtz's full-sized avatar

Louis Langholtz louis-langholtz

View GitHub Profile
@louis-langholtz
louis-langholtz / cpp-thread-safe-initialization.md
Last active November 26, 2023 06:11
C++ Requires Thread Safe Initialization of Block-Scope Variables With Static or Thread Storage Duration

C++: Thread Safe Initialization Requirements

Seems like this has come up for me enough in discussing C++ with others, that it might help to put together the information in this gist.

Standard C++ has since C++11 required that block-scope variables with static or thread storage duration get initialized in a thread safe way. Besides there being a StackOverflow answer - to a related question - showing some example assembly for C++11 that includes thread safe guards, there's also the C++ standard documents themselves.

What follows is the specific requirement by versions of the standard.

C++03

@louis-langholtz
louis-langholtz / stateless_vs_stateful_cpp_allocators.md
Last active January 27, 2024 15:01
C++: Stateless vs. Statefull Allocators

C++: Stateless vs. Stateful Allocators

Categorizing Allocators

All standard C++ library conforming allocators have to support the minimal interface requirements. Beyond these requirements, some examples of fully defined allocators are LLVM's latest standard C++ library implementation of std::allocator, and a custom allocator I've written called ThreadLocalAllocator. These are both examples of what's been categorized as "stateless" allocators. Alternatively, allocators can be "stateful".

@louis-langholtz
louis-langholtz / min_cpp_allocator.md
Last active August 22, 2023 23:56
Minimal C++ Allocator

C++: Minimal Allocator

From item 11 of section 16.5.3.5 Cpp17Allocator requirements of n4861.pdf:

"The following is an allocator class template supporting the minimal interface...":

template<class Tp>
struct SimpleAllocator {
 typedef Tp value_type;
@louis-langholtz
louis-langholtz / CppNoStdEndl.md
Last active October 24, 2023 02:40
C++: More Reasons To Avoid std::endl

C++: More Reasons To Avoid std::endl

In the realm of death by a thousand cuts, please stop using std::endl and remove it from code. Per cppreference.com, std::endl "inserts a newline character into the output sequence... and flushes it". Okay, so why exactly shouldn't it be used???

The Issues

There have been numerous articles written about endl that echo the sentiment against using it. Here's some:

@louis-langholtz
louis-langholtz / C++LovePolymorphicValueTypes.md
Last active November 9, 2023 14:35
C++: My Love Affair With Polymorphic Value Types

shape-polymorphic-value-type

C++: My Love Affair With Polymorphic Value Types

If you're developing C++ code, especially for larger projects with longer histories, "polymorphic value types" might just be the best solution to your biggest coding frustrations!

Problem

Have you worked with code with countless derived classes? Have you had to deal with lots of pointers to data structures that in turn have pointers to other data structures? Did you find yourself feeling tired from the mental hopping around you were doing in order to understand that code? If so, you're not alone.

@louis-langholtz
louis-langholtz / RtPolymorphicVisitor.md
Last active January 11, 2018 00:40
Runtime Polymorphic Visitor

Extend the runtime polymorphism to vistor like pattern...

namespace playrho {

template <>
void Visit(const d2::EdgeShapeConf& shape, void* userData)
{
    const auto shapeDrawer = static_cast<ShapeDrawer*>(userData);
 shapeDrawer-&gt;Visit(shape);
@louis-langholtz
louis-langholtz / PlayRhoCLA.md
Last active July 22, 2017 20:02
PlayRho Contributor License Agreement

Contributor License Agreement

The following terms are used throughout this agreement:

  • You - the person or legal entity including its affiliates asked to accept this agreement. An affiliate is any entity that controls or is controlled by the legal entity, or is under common control with it.
  • Project - is an umbrella term that refers to the PlayRho open source project.
  • Contribution - any type of work that is submitted to Project, including any modifications or additions to existing work.
  • Submitted - conveyed to Project via a pull request, commit, issue, or any form of electronic, written, or verbal communication with contributors or maintainers.

1. Grant of Copyright License.