I have had a highly restrictive diet for the last 20 years (no sugar, low fibre are the two big ones) and so have been dealing with this situation over and over again.
In most cases I do not mention it. I simply say that I don't want any, or I'm not much of a cake eater, I prefer savouries. Most people will let this pass and allow me to just hang out and enjoy the social occasion without anyone feeling bad or feeling they have to offer to go out of their way to accommodate me in any way.
However, if these are people that I am going to see regularly or are going to be sharing food with often then I would find a way to get the information out there. I'd do it in a different setting, at a time when no one is going to be uncomfortable about food. This will inform their choices for later events, even if it is to give you prior warning so you can bring something yourself to join in with if you wish.
It also removes the 'big reveal' of how no one has considered you when you turn up with a dairy free cake at an event and people find out why.
This is not fool proof. 20 years on many of my friends continue to forget and feel bad, but thankfully they are friends and know I am laid back about it. I also make sure that I eat before events like this or bring a snack as there is rarely food I can eat.