Boost logo

Boost Users :

From: Nevin \ (nevin_at_[hidden])
Date: 2008-05-16 19:37:38


2008/5/16 Noah Roberts <roberts.noah_at_[hidden]>:
> Nevin ":-]" Liber wrote:
>> 2008/5/16 Noah Roberts <roberts.noah_at_[hidden]
>> <mailto:roberts.noah_at_[hidden]>>:
>>
>> I don't like this idea. You are creating a dependency on the fact that
>> the called function will NOT keep a copy

Suppose I had the function:

void foo(int const* p)
{
    if (p) std::cout << *p << std::endl;
}

It is perfectly legal to call it as:

void bar()
{
    int i(2);
    foo(&i);
}

int main()
{
    bar();
    bar();
}

Now, if we let foo squirrel away a copy of p, as in:

void foo(int const* p)
{
    static int const* pp;
    if (p && pp) std::cout << *pp << ' ' << *p << std::endl;
    pp = p;
}

The program is now broken, since it is illegal to dereference pp
during the second call to foo(), yet foo has no way of knowing that.

What should the interface to foo() be such that it doesn't break
whether or not foo() keeps a copy of p?

-- 
 Nevin ":-)" Liber <mailto:nevin_at_[hidden]> (847) 691-1404

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net