Smart Pointers code that compiles in FPC and Delphi
Posted by jpluimers on 2019/03/06
Delphi and FPC have different language boundaries, so it is always good to read a thread discussing how to get near-the-edge cases work in both.
This case is about Smart Pointers (here called auto pointers, maybe because auto_ptr
is what they used to be called in C++): [WayBack] Hi all, I am trying to make a certain piece of code cross compile between Delphi and FPC. On FPC, it compiles fine but on Delphi I get a [dcc32 Error] P… – Ugochukwu Mmaduekwe – Google+
In the end if comes down to that Delphi does not allow forward-declaration of records (it does for interfaces and classes), but that you do not need a __PAutoPtr_T = ^__TAutoPtr_T
, because you can use ^__TAutoPtr_T
in parameters.
In Spring4D, they are called Shared
/IShared
/TShared
, see my comment:
Note that since these are reference counted, you might want to call them
shared_ptr
. If you enforce the single-instance behaviour, observe that in C++ they are now calleduniqe_ptr
.In Spring4D this rename has already taken place a while ago: https://bitbucket.org/sglienke/spring4d/commits/e252b81fd3788cf5b82588721f68d00c892deb87
–jeroen
rvelthuis said
” Delphi does not allow forward-declaration of records (it does for interfaces and classes”.
Of course not! Interfaces and classes are reference types (i.e. pointers), so there is no problem with them. But records are value types.
You can forward reference POINTERS to records, as long as they are in the same type block. It is done hundreds of times in many of the Delphi sources.
rvelthuis said
If you could forward record types, you could theoretically include a record in itself, which would cause some kind of infinite recursion in the compiler and which would be an impossible type.