Arithmetic operations with generic types in Delphi

前端 未结 3 711
小鲜肉
小鲜肉 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:37

    Delphi generics do not support arithmetic operators that act on generic types. In order for the compiler to accept the code it needs to know that each operation on a generic type is going to be available upon instantiation.

    Generic constraints allow you to tell the compiler what capabilities the type has. However generic constraints do not allow you to tell the compiler that the type supports arithmetjc operators.

    Unfortunately what you are trying to do is simply not possible. For sure you can construct frameworks yourself that can use tools like interfaces to get the arithmetic performed but doing so gives up performance. If that is acceptable then fine. Otherwise you are best biting the bullet and avoiding generics here.

    Oh for C++ templates.

提交回复
热议问题