Skip to content

Instantly share code, notes, and snippets.

@alf-p-steinbach
alf-p-steinbach / Cat.hpp
Created June 5, 2021 18:56
Visitor pattern in separate files
#include "Visitable.hpp"
struct Cat: Visitable
{
void hunt_birds() { throw "Hunting birds..."; }
void accept( Doer& doer ) override { doer.deal_with( *this ); }
};
@alf-p-steinbach
alf-p-steinbach / app-manifest.xml
Last active May 27, 2020 07:52
Demo of Windows API level coding
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity type="win32" name="Demo of Windows API level coding" version="1.0.0.0"/>
<application>
<windowsSettings>
<activeCodePage xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings"
>UTF-8</activeCodePage>
</windowsSettings>
</application>
<dependency>
@alf-p-steinbach
alf-p-steinbach / c++-figure.svg.cpp
Last active April 24, 2020 07:45
SVG graphics via cout (the C curve)
#include "draw_c_curve_on.hpp" // draw_c_curve_on
namespace g = graphics;
#include "util-string_from.hpp"
using util::string_from;
#include <iostream>
#include <string>
using std::cout, std::endl, std::ostream,
std::string;
@alf-p-steinbach
alf-p-steinbach / char_bitmap.hpp
Created March 3, 2020 18:23
C++ "Hello, world!" with asterisk glyphs
#pragma once
// Based on a header generated with GIMP, using Courier New font size 11.
// 8x14 cells, ASCII range 32 through 127.
namespace char_bitmap {
using Byte = unsigned char;
const int cell_width = 8;
const int cell_height = 14;
const int char_height = 9;
@alf-p-steinbach
alf-p-steinbach / lenna.hpp
Last active January 19, 2020 13:48
Gdi+ bitmap from base-64 encoded data
#pragma once
#include <string_view> // std::string_view
//---------------------------------------- Lenna image scaled down to 51x51:
namespace lenna {
using std::string_view;
const auto& data_literal = R"(
iVBORw0KGgoAAAANSUhEUgAAADMAAAAzCAIAAAC1w6d9AAAABGdBTUEAALGPC/xh
BQAAAAlwSFlzAAAOwwAADsMBx2+oZAAAABh0RVh0U29mdHdhcmUAcGFpbnQubmV0
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Why COW was deemed ungood for std::string.

COW, short for copy on write, is a way to implement mutable strings so that creating strings and logically copying strings, is reduced to almost nothing; conceptually they become free operations like no-ops.

Basic idea: to share a data buffer among string instances, and only make a copy for a specific instance (the copy on write) when that instance's data is modified. The general cost of this is only an extra indirection for accessing the value of a string, so a COW implementation is highly desirable. And so the original C++ standard, C++98, and its correction C++03, had special support for COW implementations, and e.g. the g++ compiler's std::string implementations used COW.

So why was that support dropped in C++11?

In particular, would the same reason or reasons apply to a reference counted immutable string value class?