Arithmetic operations with generic types in Delphi

前端 未结 3 682
小鲜肉
小鲜肉 2021-01-12 16:54

I\'m new in Delphi. For a project required by my company, I need to translate some code from our existing C++ classes to Delphi. Some of these classes are templates, such as

3条回答
  •  悲&欢浪女
    2021-01-12 17:44

    Delphi Generics are intrinsically different from C++ template, and resemble more their C# counterpart.

    In C++ you can do any operation on template types, and at time of the template instantiation the compiler checks that the operation you are performing in the template are available for the specific type you are using. If not you get a compiler error.

    In Delphi (and many other languages) you declare a generic type possibly providing some declarative constraints, and those constraints -- base classes or interface -- determine the operations you can do on the generic type. At instantiation time, the only check is if the declared type fits the constraint.

    Arguably, the Delphi language could add constraints for floating point or ordinal types, but this would provide a very limited flexibility (changing the floating or integer type you can use in the generic instance). I personally don't regard this as a critical feature.

提交回复
热议问题