Are there extensions for C++ like there are in C#?
For example in C# you can do:
public static uint SwapEndian(this uint value)
{
var tmp = BitConverter.GetBytes(value);
Array.Reverse(tmp);
return BitConverter.ToUInt32(tmp, 0);
}
someuint.SwapEndian();
Is there anything like that in C++?
Extension methods (and also “static classes”) exist in C#/Java languages solely because the designers decided that (the Java way of) OOP is The One True Way and that everything must be a method from a class:
This is not C++ way of doing things. In C++ you have namespaces, free functions and Koenig lookup to extend the behavior of a class:
I usually consider extension methods harmful. If you attach too much behavior to a class, you are proabably failing to capture the reason why the class exists. Also (like “partial classes”), they tend to make the code related to a class non local. Which is bad.
As to your problem, in C++ you simply do:
Usage:
or, if the type can be deduced: