Is this shampoo?

Mini-bottles in hotel bathrooms...
Am I the only one who can't read the writing on them?
I mean the font size is tiny. You can't read them.
There's just no way to tell which bottles are which.
Shampoo? Conditioner? Body lotion? Moisturiser? Shower gel?
It's unbelievable how many different bottles there are!
(But no toothpaste).
Even if you have your reading glasses on you still can't read the labels.
Not that you have your reading glasses on when you're about to take a shower!
Not at first anyway.
You get in the bathroom and you see the 18 mini-bottles!
And you think - "figgins - which one is shampoo?"
You pick one hoping the bottles in this hotel will be different.
That you'll be able to read the label.
But no. You can't.
So you walk back into your room to try and find your glasses.
Now you're buck naked, wearing nothing but your glasses, about to take a shower!
You start reading the mini-bottle labels.
The colour is #3422DE and the background colour is #3422DD.
They're so similar you cannot read anything!
It's just not physically possible.
A small defeat in todays modern world!


Reality is not what is seems

is an excellent book by Carlo Rovelli (isbn 978-0-141-98321-9)
As usual I'm going to quote from a few pages.

It doesn't describe where there is a particle but how the particle shows itself to others.
It isn't things that enter into relations but, rather, relations that ground the notion of 'thing'.
An object is a monotonous process. [Nelson Goodman]
The sun bends space around itself, and the Earth does not circle around it drawn by a mysterious distant force but runs straight in a space that inclines.
What is conserved is the sum of mass and energy, not each separately. Processes must exist that transform energy into mass, or mass into energy.
Take the equations of quantum mechanics that determines the form of the orbitals of an electron. This equation has a certain number of solutions, and these solutions correspond exactly to hydrogen, helium, oxygen ... and the other elements!
On Mars, there are events that in this precise moment have already happened, events that are yet to happen, but also a quarter of an hour during which things occur that are neither in our past nor in our future.
Time is not universal and fixed, it is something which expands and shrinks, according to the vicinity of masses.
Suppose we want to observe a very, very, very small region of space. To do this, we need to place something in this area to mark the point that we wish to consider. Say we place a particle there. Heisenberg had understood that you can't locate a particle at a point in space for long. It soon escapes. The smaller the region in which we try to locate a particle, the greater the velocity at which it escapes. This is Heisenberg's uncertainty principle.
It isn't true that we 'do not see' Faraday lines. We only see vibrating Faraday lines. "To see" is to perceive light, and light is the movement of Faraday lines.


The Ascent of Man

is an excellent book by Jacob Bronowski (isbn 0-7088-2035-2)
As usual I'm going to quote from a few pages.

Evolution is the climbing of a ladder from the simple to the complex by steps, each of which is stable in itself.
The turning point to the spread of agriculture in the Old World was almost certainly the occurrence of two forms of what with a large, full head of seeds. Before 800 BC wheat was not the luxuriant plant it is today. It was merely one of many wild grasses that spread throughout the Middle East. By some genetic accident, the wild wheat crossed with a natural goat grass and formed a fertile hybrid. ... Now we have a beautiful ear of wheat, but one which will never spread in the wind because the ear is too tight to break up.
The Principle of Uncertainty is a bad name. In science or outside it, we are not uncertain, our knowledge is merely confined within a certain tolerance. We should call it the Principle of Tolerance.
When to copper you add an even softer metal, tin, you make an alloy which is harder and more durable than either - bronze. ... Almost any pure material is weak, and many impurities will do to make is stronger.
The making of the sword, like all ancient metallurgy, is surrounded with ritual, and that is for a clear reason. When you have no written language, when you have nothing that can be called a chemical formula, then you must have a precise ceremonial which fixes the sequence of operations so that they are exact and memorable.
To us gold is precious because it is scarce; but to the alchemists, all over the world, gold was precious because it was incorruptible. ... every medicine to fight old age contained gold, metallic gold, as an essential ingredient, and the alchemists urged their patrons to drink from gold cups to prolong life.
We still use for the female the alchemical symbol for copper, that is, what is soft: Venus. And we use for the male the alchemical sign for iron, that is, what is hard: Mars.
When the Bible says three wise men followed a star to Bethlehem, there sounds in the story the echo of an age when wise men were stargazers.
Three thousand years after they were made, the village women of Khuzistan still draw their water ration from the qanats.
The Greeks when they saw the Scythian riders believed the horse and the rider to be one; that is how they invented the legend of the centaur.
Galileo is the creator of the modern scientific method... he really did for the first time what we think of as practical science: build the apparatus, do the experiment, publish the results.
Relativity is the understanding of the world not as events but as relations.
He [Einstein] hated war, and cruelty, and hypocrisy, and above all he hated dogma.
It was because [James] Brindley could not spell the world 'navigator' that workmen who dig trenches or canals are still called 'navvies'.
Always [Leo] Szilard wanted the [atom] bomb to be tested openly before the Japanese and an international audience, so that the Japanese should know its power and should surrender before people died.


What is Life?

is an excellent book by Paul Nurse (isbn 978-1-788451-40-6)
As usual I'm going to quote from a few pages.

Cells repair these mutations, but they are not completely successful. If they were, all individuals of a species would be identical and evolution would stop. This means the error rate itself is subject to natural selection. If that error rate is too high the information stored by the genome will degenerate and become meaningless, and if errors are too rare, the possibility for evolutionary change is reduced. Over the long term, the most successful species will be those that can maintain the right balance between constancy and change.
Humanity should care about the entire biosphere; all the different life forms that share our planet are our relatives.
Once microbes evolved the ability to photosynthesize, they multiplied, over the millenia, to such an extent that the amount of oxygen in the atmosphere spiked. What followed, between 2 and 2.4 billion years ago, is called the Great Oxygen Catastrophe. All organisms that existed at that time were microbes, either bacteria or archaea, but some researchers think most of them were wiped out by the appearance of all that oxygen. It is ironic that life created conditions that nearly ended life as a whole.
To fuel all of the chemical reactions needed to support your body's trillions of cells, your mitochondria together produce, amazingly, the equivalent of your entire bodyweight in ATP every day!
The life forms that survive natural selection persist because they work, not necessarily because they do things in the most efficient or straightforward way possible. ... This can disturb some physicists who turn their attentions to biology. Physicists tend to be attracted to elegant, simple solutions, and can be less comfortable with the messy and less-than-perfect reality of living systems.
Even plants rely on bacteria found in or near their roots that capture nitrogen from the atmosphere. ... In fact, this is something that, as far as we know, no eukaryote can do for itself. That means there is not a single known species of animal, plant or fungus that can generate its own cellular chemistry entirely from scratch.
Life on Earth belongs to a single, vastly interconnected ecosystem, which incorporates all living organisms.
Biology shows us that all the living organisms we know of are related and closely interacting. We are bound by a deep connectedness to all other life.


Sapiens. A Brief History of Human Kind

is an excellent book by Yuval Noah Harari (isbn 978-0-099-59008-8)
As usual I'm going to quote from a few pages.

Whereas chimpanzees spend five hours a day chewing raw food, a single hour suffices for people eating cooked food.
Since long intestines and large brains are both massive energy consumers, it's hard to have both. By shortening the intestines and decreasing their energy consumption, cooking inadvertently opened the way to the jumbo brains of Neanderthals and Sapiens.
Large numbers of strangers can cooperate successfully by believing in common myths.
There is some evidence that the size of the average Sapiens brain has actually decreased since the age of foraging. Survival in that era required superb mental abilities from everyone.
What characterises all these acts of communication is that the entities being addressed are local beings. They are not universal gods, but rather a particular deer, a particular tree, a particular stream, a particular ghost.
The extra food did not translate into a better diet or more leisure. Rather, it translated into population explosions and pampered elites.
The new agricultural tasks demanded so much time that people were forced to settle permanently next to their wheat fields.
This is the essence of the Agricultural Revolution: the ability to keep more people alive under worse conditions.
One of history's few iron laws is that luxuries tend to become necessities and to spawn new obligations.
Evolution is based on difference, not on equality.
There is not a single organ in the human body that only does the job its prototype did when it first appeared hundreds of millions of years ago.
The mere fact that Mediterranean people believed in gold would cause Indians to start believing in it as well. Even if Indians still had no real use for gold, the fact that Mediterranean people wanted it would be enough to make the Indians value it.
The first religious effect of the Agricultural Revolution was to turn plants and animals from equal members of a spiritual round table into property.
The monotheist religions expelled the gods through the front door with a lot of fanfare, only to take them back in through the side window. Christianity, for example, developed its own pantheon of saints, whose cults differed little from those of the polytheistic gods.
Level two chaos is chaos that reacts to predictions about it, and therefore can never be predicted accurately.
In many societies, more people are in danger of dying from obesity than from starvation.
Each year the US population spends more money on diets than the amount needed to feed all the hungry people in the rest of the world.
Throughout history, the upper classes always claimed to be smarter, stronger and generally better than the underclasses... With the help of new medical capabilities, the pretensions of the upper classes might soon become an objective reality.


The Culture Code

is an excellent book by Daniel Coyle (isbn 978-1-847-94127-5)
As usual I'm going to quote from a few pages.

Much of the connection happens around the dinner table, as Popovich is obsessed with food and wine.
One misconception about highly successful cultures is that they are happy, lighthearted places. This is mostly not the case. They are energized and engaged, but at their core their members are oriented less around achieving happiness than around solving hard problems together.
Allen could find none that played a meaningful role in cohesion. Except for one. The distance between their desks.
For the vast majority of human history, sustained proximity has been an indicator of belonging.
It's important to avoid interruptions. The smoothness of turn taking, as we've seen, is a powerful indicator of cohesive group performance.
The groups I studied had extremely low tolerance for bad apple behaviour.
The groups I visited were uniformly obsessed with design as a lever for cohesion and interaction.
He also had the company buy nicer coffee machines and install them in more convenient gathering places.
Merely replacing four-person tables with ten-person tables has boosted productivity by 10 percent.
Kauffman decreed that every aspect of training be team-based.
It's very hard to be empathic when you're talking.
The road to success is paved with mistakes well handled.
We should have made the hallways wider. We should have made the cafe bigger.


Life, the Universe and Everything

is an excellent book by Douglas Adams (isbn 978-0-330-49120-4). As usual I'm going to quote from a few pages.

He would insult the universe. That is, he would insult everybody in it. Individually, personally, one by one, and (this was the thing that he really decided to grit his teeth over) in alphabetical order.
'Eddies,' said Ford, 'in the space-time continuum.'
'Ah, nodded Arthur, 'is he? Is he?'
It was a charming and delightful day at Lord's as Ford and Arthur tumbled haphazardly out of a space-time anomaly and the immaculate turf rather hard.
'A what?' he said.
'An SEP.'
'An S...?'
'...EP.'
'And what's that?'
'Somebody Else's Problem,' said Ford.
The second non-absolute number is the given time of arrival, which is now known to be one those most bizarre of concepts, a recipriversexcluson, a number whose existence can only be defined as being anything other than itself. In other words, the given time of arrival is the one moment of time at which it is impossible that any member of the party will arrive.
'Tell me it was a coincidence, Dent' it said. 'I dare you to tell me it was a coincidence!' 'It was a coincidence,' said Arthur quickly.
It was the Cathedral of Hate.
It was the product of a min that was not merely twisted, but actually sprained.
It was huge. It was horrific.
It had a Statue in it.
We will come to the Statue in a moment.
'I got yanked involuntarily back into the physical word,' pursued Agrajag, 'as a bunch of petunias. In, I might add, a bowl.'
Strictly speaking, all editors since Lig Lury Jr have therefore been designated Acting Editors, and Lig's desk is still preserved the way he left it, with the addition of a small sign which says 'Lig Lury Jr, Editor, Missing, presumed Fed'.
The longest and most destructive party ever held is now into its fourth generation and still no one shows any signs of leaving.


The Restaurant at the End of the Universe

is an excellent book by Douglas Adams (isbn 978-0-330-49121-1). As usual I'm going to quote from a few pages.

There is another theory which states that this has already happened.
The story so far: In the beginning the Universe was created. This had made a lot of people very angry and been widely regarded as a bad move.
The motto stands - or rather stood - in three-mile high illuminated letters near the Complaints Department spaceport on Eadraz. Unfortunately its weight was such that shortly after it was erected, the ground beneath the letters caved in and they dropped for nearly half their length through the offices of many talented young complaints executives - now deceased.
Quite how Zaphod Bebblebrox arrived at the idea of holding a seance at this point is something he was never quite clear on.
'Listen, three eyes,' he said', 'don't you try to outweird me, I get stranger things than you free with my breakfast cereal.'
Marvin was forced to say something which came very hard to him. 'I don't know,' he said.
'I go up,' said the elevator, 'or down.'
'Good,' said Zaphod. 'We're going up.'
'Or down,' the elevator reminded him.
And the worse they were to wear, the more people had to buy to keep themselves shod, and the more the shops poliferated, until the whole economy of the place passed what I believe is termed the Shoe Event Horizon, and it became no longer economically possible to build anything other than shoe shops.
'Transtellar Cruise Lines would like to apologize to passengers for the continuing delay to this flight. We are currently awaiting the loading of our complement of small lemon-soaked napkins for your comfort, refreshment and hygiene during the journey. Meanwhile we thank you for your patience.'
In it, guests take (willan on-take) their places at table and eat (willan on-eat) sumptuous meals whilst watching (willing watchen) the whole of creation explode around them.
'You've never heard of Disaster Area?'
'It says "Golgafrinchan Ark Fleet, Ship B, Hold Seven, Telephone Sanitizer Second Class" - and a serial number.'
To summarize the summary: anyone who is capable of getting themselves made President should on no account be allowed to do the job.
Their track suits were now all dirty and even torn, but they all had immaculately styled hair.


The Hitch Hiker's Guide to the Galaxy

is an excellent book by Douglas Adams (isbn 978-0-330-49119-8). As usual I'm going to quote from a few pages.

Far out in the uncharted backwaters of the unfashionable end of the Western Spiral Arm of the Galaxy lies a small unregarded yellow sun.
The Guide also tells you on which planets the best Pan Galactic Gargle Blasters are mixed, how much you can expect to pay for one and what voluntary organizations exist to help you rehabilitate afterwards.
People of Earth, your attention please. This is Prostetnic Vogon Jeltz of the Galactic Hyperspace Planning Council...
The practical upshot of all this is that if you stick a Babel fish in your ear you can instantly understand anything said to you in any form of language.
The prisoners sat in the Poetry Appreciation chars - strapped in.
'Space', it says, 'is big. Really big. You just won't believe how vastly hugely mindbogglingly big it is. I mean you may think it's a long way down the road to the chemist, but that's just peanuts to space. Listen...' and so on.
The principle of generating small amounts of finite improbability by simply hooking the logic circuits of a Bambleweeny 57 Sub-Meson Brain to an atomic vector plotter suspended in a strong Brownian Motion producer (say a nice hot cup of tea) were of course well understood.
Here I am, brain the size of a planet and they ask me to take you down to the bridge. Call that job satisfaction? Cos I don't.
For years radios had been operated by means of pressing buttons and turning dials; then as technology became more sophisticated the controls were made touch sensitive - you merely had to brush the panels with your fingers; now all you had to do was wave your hand in the general direction of the components and hope. It saved a lot muscular expenditure of course, but meant that you had to sit infuriatingly still if you wanted to keep listening to the same programme.
'Oh God,' said Zaphod. He hadn't worked with this computer for long but had already learned to loathe it.
'Computer!' shouted Zaphod. 'Rotate angle of vision through one-eighty degrees and don't talk about it!'
Many men of course became extremely rich, but this was perfectly natural and nothing to be ashamed of because no one was really poor - at least no one worth speaking of.
'Hi there! This is Eddie your shipboard computer, and I'm feeling just great, guys, and I know I'm just going to get a bundle of kicks out of any program you care to run through me.'
'You just let the machines get on with the adding up,' warned Majikthise, 'and we'll take care of the eternal verities, thank you very much. You want to check your legal position you do, mate. Under the law the Quest for the Ultimate Truth is quite clearly the inalienable prerogative of your working thinkers.'
'I think the problem, to be quite honest with you, is that you've never actually known what the question is.'
R is a velocity measure, defined as a reasonable speed of travel that is consistent with health, mental wellbeing and not being more than say five minutes late.


How to read water

is an excellent book by Tristan Gooley (isbn 978-1-473-61522-9). As usual I'm going to quote from a few pages:
One of the universal truths of human observation is that we see more of what we expect to see and less of what we don't expect to see.
Much of my work is not about teaching people to see things that are hard to see, but in showing them how to notice the things that hide in plain sight.
It did not take sailors long to work out that a ship that carries too much may be vulnerable in heavy seas, but sailors were rarely the ones to make the decision about how much cargo a ship could safely carry. The merchants making the profit would have had a different view to the deckhand, especially if the trader never set foot on the vessel. This led to a wrestle between greedy traders and cautious captains that lasted centuries. The first attempts to regulate how much a ship could carry go back over four thousand years to ancient Crete.
Samuel Plimsoll, a nineteenth-century English politician, realized that a low freeboard height can present a problem, but he also appreciated that it becomes the solution if we take a very keen interest in it. In other words, we can tell if there is too much cargo in the boat by looking much more carefully at how high the water rises up the side of the hull. And the easiest way to do this is by drawing a ruler on the side of the ship, calibrated according to an architect's or engineer's understanding of the boat. These lines, which became known as Plimsoll Lines, were such a simple and brilliant success that they became law and proliferated around the world.
From 1833, when the first tide tables were produced by the Admiralty, the emphasis shifted from looking, thinking and understanding, to depending on tables of others' measurements.
There is a stange truth in the profile of beaches: they have evolved in a physical sense to be a near ideal shape to defend themselves against the onslaught of the sea. This means that almost any attempt to engineer a 'solution' to what nature is trying to achieve has as much chance of backfiring as working.
Many sailors use little pieces of fabric, nicknamed 'tell-tails', that are tied to the sails and stays (the wires that give the mast stability), to offer a constant visual reminder of what the wind is doing.
Once the depth of the water is half the wavelength of the waves, it effectively cramps the motion of the waves and it is this that slows them down.
Sailors dislike precision almost as much as they like bureaucracy.
Rivers do not run straight for more than ten times their own width.
There will be an alternating combination of quick water and much slower water and this always happens in a certain way. The quick patches are knowns, perhaps onomatopoeically, as 'riffles' and the slower areas are known as pools. If there is no human tinkering with a river's flow, then there will be a riffle-pool sequence for every stretch of river that is fives times its width.
It is typical for the water at the sides of a river to be travelling at only a quarter of the speed of the water in the centre. The river is being slowed by two things at its sides; when it comes into contact with banks it is slowed by friction and it is also slowed by the shallowing at the sides.
A stream is just a river you can step over.
Swell is the name of the waves that have enough energy to travel beyond the area of wind.


Organize for Complexity

is an excellent book by Niels Pflaeging (isbn 978-0-9915376-0-0). As usual I'm going to quote from a few pages:
What Taylor pioneered was the idea of consistently dividing an organization between thinking people (managers) and executing people (workers).
Problem-solving in a life-less system is about instruction.
Problem-solving in a living system is about communication.
Any attempt to motivate can only lead to de-motivation.
Ultimately, organizing for complexity and self-organization are always about empowering teams ... not about empowering individuals.
Actual teams of people working for and with each other.
Nobody is in control. Everybody is in charge.
To be intensively involved in selection [recruiting] is a matter of honor.
A hallmark of great selection is that it is highly time-consuming.
Management is a mindset that will not just go away all by itself.
When employees think for themselves and make entrepreneurial decisions automonomously, you must at all times bear joint reponsibility for those decisions, even if you or other members of the organization might have decided differently.
A "beta" kind of organization produces many such stories: Peculiarities, unusual practices, by which they can be instantly recognized among so many over-managed and under-led organizations.
People do not need to be forced to work. However, this deeply-seated prejudice about people and their relationship to work is what keeps management alive.


The book of five rings

is an excellent book by Miyamoto Musashi, translated by Thomas Cleary (isbn 1-57062-748-7). As usual I'm going to quote from a few pages:
Preface: In common parlance, to do something with a real sword means to do it with utmost earnestness… The Book of Five Rings… explicitly intended to symbolise processes of struggle and mastery in all concerns and walks of life.
The martial way of life practiced by warriors is based on excelling others in anything and everything.
Fourth is the way of the artisan. In terms of the way of the carpenter, this involves skilful construction of all sorts of tools, knowing how to use each tool skilfully, drawing up plans correctly by means of the square and the ruler, making a living by diligent practice of the craft… practice unremittingly.
You should observe reflectively, with overall awareness of the large picture a well as precise attention to small details.
Having attained a principle, one detaches from the principles; thus one has spontaneous independence in the science of martial arts and naturally attains marvels.
As human beings, it is essential for each of us to cultivate and polish our individual path.
Observation and perception are two separate things.
It is essential to be relaxed in body and mind.
If you get to feeling snarled up and are making no progress, you toss your mood away and think in your heart that you are starting everything anew.
In my military science, it is essential that the physical aspect and the mental state both be simple and direct.
Whether in large- or small-scale military science, there is no narrow focus of the vision. As I have already written, by finicky narrowness of focus, you forget about bigger things and get confused, thus letting certain victory escape you.
Things stick in your mind because of being in doubt.
The practice of all the arts is for the purpose of clearing away what is on your mind. In the beginning, you do not know anything, so paradoxically you do not have any questions on your mind. Then, when you get into studies, there is something on your mind and you are obstructed by that. This makes everything difficult to do.


compile time assertions in C

C has a facility for checking dynamic assertions at run-time. It's inside <assert.h> and its called assert. Now assert is a macro, so why isn't it called ASSERT? I don't know. Prior art no doubt. Anyway, assert is a dynamic runtime feature, you can only use it inside functions.

/* not inside a function, won't compile :-( */
assert(sizeof(int) * CHAR_BIT >= 32);  

That's a pity because it would be nice if I could get the compiler to check things like this automatically at compile time. I've occasionally seen an attempt at a compile-time check like this...

#if sizeof(int) * CHAR_BIT < 32
#error People of Earth. Your attention please...
#endif

But this doesn't work. The C preprocessor is a glorified text reformatter: it knows practically nothing about C. However, there is a way to write this as a compile time assertion (and moving any error trap to an earlier phase is a Good Thing)

{ yourself }

#define COMPILE_TIME_ASSERT(expr)   \
    char constraint[expr]

COMPILE_TIME_ASSERT(sizeof(int) * CHAR_BIT >= 32);

What's is going on here? Well, the example preprocesses to...

char constraint[sizeof(int) * CHAR_BIT >= 32];

If the expression is true, (an int is at least 32 bits), the expression will have a value of one, and constraint will be an array of one char. If the assertion is false, (an int is less than 32 bits), the expression will have a value of zero, and constraint will be an empty array. That's illegal, and you'll get a compile time error. Viola, a compile time assertion :-) You can use it inside and outside a function but you can't use it twice in the same function, as you end up with a duplicate definition. To solve that problem you could resort to some convoluted macro trickery:

#define COMPILE_TIME_ASSERT(expr)       char UNIQUE_NAME[expr]
#define UNIQUE_NAME                     MAKE_NAME(__LINE__)
#define MAKE_NAME(line)                 MAKE_NAME2(line)
#define MAKE_NAME2(line)                constraint_ ## line

But this is pretty horrible. Also, you will probably get warnings about unused variables. Take a step back for a moment and think about why it works at all. It's because you have to specify the size of an array as a compile time constant. The formal grammar of a direct-declarator tells you this. Let's look at some bits of grammar more closely:

Constrained arrays

 direct-declarator:
  identifier
  ( declarator )
  direct-declarator [ constant-expression opt ]
  direct-declarator ( parameter-type-list )
  direct-declarator ( identifier-list opt )

I just piggy backed on this, using the constraint that the value of the constant expression cannot (in this context) be zero. A natural question (to the curious) is are there other parts of the formal grammar that require a constant expression. The answer, of course, is yes.

Constrained enums

 enumerator:
  enumeration-constant
  enumeration-constant = constant-expression

However, I can't use this because there are no useful constraints in this context.

Constrained bit-fields

 struct-declarator:
  declarator
  declarator opt : constant-expression

Reading the constraints of a bit field I see that if the width of a bit-field is zero the declaration cannot have a declarator. In other words this is legal...

 struct x { unsigned int : 0; };

but this is not...

 struct x { unsigned int bf : 0; };

This suggests another way to create a compile time assertion

#define COMPILE_TIME_ASSERT(expr)   \
    struct x { unsigned int bf : expr; }

COMPILE_TIME_ASSERT(sizeof(int) * CHAR_BIT >= 32);

Trying this we again get duplicate definitions, not of a variable this time, but of the type struct x. However we can fix this by creating an anonymous struct:

#define COMPILE_TIME_ASSERT(expr)   \
    struct { unsigned int bf : expr; }

This works. However, now you'll probably get warnings about the unused untagged struct.

There is one last bit of grammar that uses a constant-expression.

Constrained switch

 labelled-statement:
  identifier : statement
  case constant-expression : statement
  default : statement

It's well known that you can't have two case labels with the same constant. The following will not compile...

switch (0)
{
case 0:
case 0:;
}
So, here's yet another way to create a compile time assertion. This time we don't create a dummy variable, or a dummy type, but a dummy statement. A dummy switch statement:
#define COMPILE_TIME_ASSERT(pred)       \
    switch(0){case 0:case pred:;}

COMPILE_TIME_ASSERT(sizeof(int) * CHAR_BIT >= 32);

If pred evaluates to true (i.e., 1) then the case labels will be 0 and 1. Different; Ok. If pred evaluates to false (i.e., 0) then the case labels will be 0 and 0. The same; Compile time error. Viola. However, a switch statement cannot exist in the global scope. So the last piece of the puzzle is to put the compile time assertions inside a function.

#include <limits.h>

#define COMPILE_TIME_ASSERT(pred)            \  
    switch(0){case 0:case pred:;}

#define ASSERT_MIN_BITSIZE(type, size)       \
    COMPILE_TIME_ASSERT(sizeof(type) * CHAR_BIT >= size)

#define ASSERT_EXACT_BITSIZE(type, size)     \
    COMPILE_TIME_ASSERT(sizeof(type) * CHAR_BIT == size)

void compile_time_assertions(void)
{
    ASSERT_MIN_BITSIZE(char,  8);
    ASSERT_MIN_BITSIZE(int,  16);
    ASSERT_EXACT_BITSIZE(long, 32);
}

C# struct/class differences

Events are locked?

Events declared in a class have their += and -= access automatically locked via a lock(this) to make them thread safe (static events are locked on the typeof the class). Events declared in a struct do not have their += and -= access automatically locked. A lock(this) for a struct would not work since you can only lock on a reference type expression.

Exist on stack or heap?

Value type local instances are allocated on the stack. Reference type local instances are allocated on the heap.

Can cause garbage collection?

Creating a struct instance cannot cause a garbage collection (unless the constructor directly or indirectly creates a reference type instance) whereas creating a reference type instance can cause garbage collection.

Meaning of this?

In a class, this is classified as a value, and thus cannot appear on the left hand side of an assignment, or be used as a ref/out parameter. For example:

class Indirect
{
    //...
    public void Method(Indirect that)
    {
        RefParameter(ref this); // compile-time error
        OutParameter(out this); // compile-time error
        this = that;            // compile-time error
    }
    //...
}
In a struct, this is classified as an out parameter in a constructor and as a ref parameter in all other function members. Thus it is possible to modify the entire structure by assigning to this or passing this as a ref/out parameter. For example:
struct Direct
{
    //...
    public void Reassign(Direct that)
    {
        RefParameter(ref this); // compiles ok
        OutParameter(out this); // compiles ok
        this = that;            // compiles ok
    }
    //...
}
Furthermore, you can reassign a whole struct even when the struct contains readonly fields!
struct Direct
{
    public Direct(int value)
    {
        Field = value;
    }

    public void Reassign(Direct that)
    {
        RefParameter(ref this); // compiles ok
        OutParameter(out this); // compiles ok
        this = that;            // compiles ok
    }

    public readonly int Field;
}

class Show
{
    static void Main()
    {
        Direct s = new Direct(42);
        Console.WriteLine(s.Field); // writes 42
        s.Reassign(new Direct(24));
        Console.WriteLine(s.Field); // writes 24
    }
}
Note however that when you call a method on a readonly value-type field, the method call is made on a copy of the field.
struct Direct
{
    // as above
}

class Caller
{
    public void Method()
    {
        Console.WriteLine(d.Field); // writes 42
        d.Reassign(new Direct(24));
        Console.WriteLine(d.Field); // writes 42!
    }

    private readonly Direct d = new Direct(42);
}

class Show
{
    static void Main()
    {
        Caller c = new Caller();
        c.Method();
    }
}

Always have a default constructor?

A struct always has a built-in public default constructor.

class DefaultConstructor
{
    static void Eg()
    {
          Direct   yes = new   Direct(); // always compiles ok
        InDirect maybe = new InDirect(); // compiles if c'tor exists and is accessible
        //...
    }
}
This means that a struct is always instantiable whereas a class might not be since all its constructors could be private.
class NonInstantiable
{
    private NonInstantiable() // ok
    {
    }
}

struct Direct
{
    private Direct() // compile-time error
    {
    }
}

Default construction triggers static constructor?

A structs static constructor is not triggered by calling the structs default constructor. It is for a class.

struct Direct
{
    static Direct()
    {
        Console.WriteLine("This is not written");
    }
}

class NotTriggered
{
    static void Main()
    {
        Direct local = new Direct();
    }
}

Can be null?

A struct instance cannot be null.

class Nullness
{
    static void Eg(Direct s, Indirect c)
    {
        if (s == null) ... // compile-time error
        if (c == null) ... // compiles ok
    }
}

Use with the as operator?

A struct type cannot be the right hand side operand of the as operator.

class Fragment
{
    static void Eg(Direct s, Indirect c)
    {
          Direct  no = s as   Direct; // compile-time error
        InDirect yes = c as InDirect; // compiles ok
        //...
    }
}

Can be locked?

A struct type expression cannot be the operand of a lock statement.

class LockStatement
{
    static void Eg(Direct s, InDirect c)
    {
        lock(s) { ... } // compile-time error
        lock(c) { ... } // compiles ok
    }
}

Can have a destructor?

A struct cannot have a destructor. A destructor is just an override of object.Finalize in disguise, and structs, being value types, are not subject to garbage collection.

struct Direct
{
    ~Direct() {} // compile-time error
}
class InDirect
{
    ~InDirect() {} // compiles ok
}

And the CIL for ~Indirect() looks like this:

.method family hidebysig virtual instance void
        Finalize() cil managed
{
  // ...
} // end of method Indirect::Finalize

Default field layout?

The default [StructLayout] attribute (which lives in the System.Runtime.InteropServices namespace) for a struct is LayoutKind.Sequential whereas the default StructLayout for a class is LayoutKind.Auto. (And yes, despite its name you can tag a class with the StructLayout attribute.) In other words the CIL for this:

public struct Direct
{
    //...
}

looks like this:

.class public sequential ansi sealed beforefieldinit Direct
       extends [mscorlib]System.ValueType
{
  //...
}

whereas the CIL for this:

public sealed class InDirect
{
    //...
}

looks like this:

.class public auto ansi sealed beforefieldinit Indirect
       extends [mscorlib]System.Object
{
    //...
}

Can be a volatile field?

You can't declare a user-defined struct type as a volatile field but you can declare a user-defined class type as a volatile field.

class Bad
{
    private volatile Direct field; // compile-time error
}
class Good
{
    private volatile Indirect field; // compiles ok
}

Can have synchronized methods?

You can't use the [MethodImpl(MethodImplOptions.Synchronized)] attribute on methods of a struct type (if you call the method you get a runtime TypeLoadException) whereas you can use the [MethodImpl(MethodImplOptions.Synchronized)] attribute on methods of a class type.

using System.Runtime.CompilerServices;

class Indirect
{
    [MethodImpl(MethodImplOptions.Synchronized)] // compiles and runs ok
    public void Method()
    {
        //...
    }
}

struct Direct
{
    [MethodImpl(MethodImplOptions.Synchronized)] // compiles ok, runtime TypeLoadException
    public void Method()
    {
        //...
    }
}

Can be pointed to?

Clause 25.2 of the C# standard defines an unmanaged type as any type that isn't a reference type and doesn't contain reference-type fields at any level of nesting. That is, one of the following:

  • Any simple value type (11.1.3, eg byte, int, long, double, bool, etc).
  • Any enum type.
  • Any pointer type.
  • Any user-defined struct-type that contains fields of unmanaged types only.
You can never take the address of a instance of a type that is not unmanaged (a fixed variable 25.3).
class Bad
{
    static void Main()
    {
        Indirect variable = new Indirect();
        unsafe
        {
            fixed(Indirect * ptr = &variable) // compile-time error
            {
                //...
            }
        }
    }
}
If you want to fix an unmanaged instance you have to do so by fixing it through an unmanaged field. For example:
class Indirect
{
    public int fixHandle;
}
class Bad
{
    static void Main()
    {
        Indirect variable = new Indirect();
        unsafe
        {
            fixed(int * ptr = &variable.fixHandle) // compiles ok
            {
                //...
            }
        }
    }
}
In contrast, you can (nearly) always take the address of an unmanaged instance.
struct Direct
{
    // no reference fields at any level of nesting
}
class SimpleCase
{
    static void Main()
    {
        Direct variable = new Direct();
        unsafe
        {
            Direct * ptr = &variable; // compiles ok
            //...       
        }
    }
}
However, you have to take the address inside a fixed statement if the variable is moveable (subject to relocation by the garbage collector, see 25.3 and example above). Also, you can never take the address of a volatile field.

So, in summary, you can never create a pointer to a class type but you sometimes create a pointer to a struct type.

Can be stackalloc'd?

You can only use stackalloc on unmanaged types. Hence you can never use stackalloc on class types. For example:

class Indirect
{
    //...
}
class Bad
{
    static void Main()
    {
        unsafe
        {
            Indirect * array = stackalloc Indirect[42]; // compile-time error
            //...
        }
    }
}
Where as you can use stackalloc on struct types that are unmanaged. For example:
struct Direct
{
    // no reference fields at any level of nesting
}
class Good
{
    static void Main()
    {
        unsafe
        {
            Direct * array = stackalloc Direct[42]; // compiles ok
            //...
        }
    }
}

Can be sizeof'd?

You can only use sizeof on unmanaged types. Hence you can never use sizeof on class types. For example:

class Indirect
{
    //...
}
class Bad
{
    static void Main()
    {
        unsafe
        {
            int size = sizeof(Indirect); // compile-time error
            //...
        }
    }
}
Where as you can use sizeof on struct types that are unmanaged. For example:
struct Direct
{
    // no reference fields at any level of nesting
}
class Good
{
    static void Main()
    {
        unsafe
        {
            int size = sizeof(Direct); // compiles ok
            //...
        }
    }
}

How to initialize fields?

The fields of a class have a default initialization to zero/false/null. The fields of a struct have no default value.

struct Direct
{
    public int Field;
}

class Indirect
{
    public Indirect()
    {
    }
    //...
    public int Field;
}

class Defaults
{
    static void Main()
    {
        Direct s;
        Console.WriteLine(s.Field);  // compile-time error

        Indirect c = new Indirect();
        Console.WriteLine(c.Field); // compiles ok
    }
}

You can initialize fields in a class at their point of declaration. For example:

class Indirect
{
    //...
    private int field = 42;
}
You can't do this for fields in a struct. For example:
struct Direct
{
    //...
    private int field = 42; // compile-time error
}
Fields in a struct have to be initialized in a constructor. For example:
struct Direct
{
    public Direct(int value)
    {
        field = value;
    }
    //...
    private int field; // compiles ok
}
Also, the definite assignment rules of a struct are tracked on an individual field basis. This means you can bypass initialization and "assign" the fields of a struct one a time. For example:
struct Direct
{
    public int X, Y;
}
class Example
{
    static void Main()
    {
        Direct d;
        d.X = 42;
        Console.WriteLine(d.X); // compiles ok
        Console.WriteLine(d.Y); // compile-time error
    }
}

Inheritance differences?

  • a struct is implicitly sealed, a class isn't.
  • a struct can't be abstract, a class can.
  • a struct can't call : base() in its constructor whereas a class with no explicit base class can.
  • a struct can't extend another class, a class can.
  • a struct can't declare protected members (eg fields, nested types) a class can.
  • a struct can't declare abstract function members, an abstract class can.
  • a struct can't declare virtual function members, a class can.
  • a struct can't declare sealed function members, a class can.
  • a struct can't declare override function members, a class can. The one exception to this rule is that a struct can override the virtual methods of System.Object, viz, Equals(), and GetHashCode(), and ToString().

Equals behavior?

classes inherit Object.Equals which implements identity equality whereas structs inherit ValueType.Equals which implements value equality.

using System.Diagnostics;

struct Direct
{
    public Direct(int value)
    {
        field = value;
    }
    private int field;
}

class Indirect
{
    public Indirect(int value)
    {
        field = value;
    }
    private int field;
}

class EqualsBehavior
{
    static void Main()
    {
        Direct s1 = new Direct(42);
        Direct s2 = new Direct(42);

        Indirect c1 = new Indirect(42);
        Indirect c2 = new Indirect(42);

        bool structEquality = s1.Equals(s2);
        bool classIdentity  = !c1.Equals(c2);

        Debug.Assert(structEquality);
        Debug.Assert(classIdentity);

    }
}
Overriding Equals for structs should be faster because it can avoid reflection and boxing.
struct Direct
{
    public Direct(int value)
    {
        field = value;
    }

    public override bool Equals(object other)
    {
        return other is Direct && Equals((Direct)other);
    }

    public static bool operator==(Direct lhs, Direct rhs)
    {
        return lhs.Equals(rhs);
    }

    //...    

    private bool Equals(Direct other)
    {
        return field == other.field;
    }

    private int field;
}