Generics in C# look inferior to templates (especially to concepts) in C++, however now and then you can build a wonderful pieces the way a C++ profi would envy.
Consider a generic converter method: T Convert<T>(object value).
T Convert<T>(object value)
In C++ I would create several template specializations for all supported conversions. Well, to make things harder, think of converter provider supporting conversion:
public interface IConverterProvider { Converter<object, T> Get<T>(); }
That begins to be a puzzle in C++, but C# handles it easily!
My first C#'s implementation was too naive, and spent too many cycles in provider, resolving which converter to use. So, I went on, and have created a sofisticated implementation like this:
private IConverterProvider provider = ... public T Convert<T>(object value) { var converter = provider.Get<T>(); return converter(value); } ... public class ConverterProvider: IConverterProvider { public Converter<object, T> Get<T>() { return Impl<T>.converter; } private static class Impl<T> { static Impl() { // Heavy implementation initializing converters. converter = ... } public static readonly Converter<object, T> converter; } }
Go, and do something close in C++!
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u