Re:Re: char* literal strings are the devil's work...
"Alisdair Meredith[CodeGear]" <
XXXX@XXXXX.COM >writes:
Quote
Hendrik Schober wrote:
>>[The problem is to warn that a reference with static duration to an
>>object with a scoped lifetime]
>
>I run into a parse error on this one.
The problem is a static duration reference has a lifetime extending to
the end of the program. If it is bound to an object with a more
deterministic lifetime (local scope, or dynamic allocation) then there
is a strong risk of holding a reference to a non-existant object. The
big risk here is when a function accepts an argument by reference, and
binds that to a locally scoped static duration reference. There is no
way to identify at the call point that the object must also be static
duration, and no way inside the function to know it is called 'safely'.
I wouldn't say there is "no way", since you are the compiler and you
can see things mortals cannot. For example, if the function that is
being called is defined in the same translation unit of the code, then
it can take a peek inside. So there are at least special cases where
it can easily be done.
To know that an extern function is returning a reference, there will
have to be a lot of intelligence added to the compiler and linker,
with decorated function meta-information in the .obj, and the linker
reading that information and reacting. It'd almost be a interpreted
language running at compile time, between the compiler and linker.
A function call that needs the linker to resolve can have additional
information, like a declared dependency on the called function having
a return value of static duration. The function definition can have
decorating meta-data describing attributes of the function, including
the duration of the return value (if known.) If it cannot be
determined, then the dependency fails and a warning should be
generated, etc.
Possible, but hard. :)
--
Chris (TeamB);