On Tue, 2014-03-04 at 23:54 -0300, Fernando Pelliccioni wrote:
>
> We still have the problem of dangling references.
> 
> 
> Any decent compiler can deal with this problem, and according to the
> Standard the implementations are encouraged to issue a warning in such
> a case.
> I don't know implementations that don't do it.
> 
> 
> GCC:
>     In function 'int& dangling()':
>     warning: reference to local variable 'i' returned
> [-Wreturn-local-addr]
>         int i = 1234;
>             ^
> Clang
>     warning: reference to stack memory associated with local variable
> 'i' returned [-Wreturn-stack-address]
>         return i;
>                ^
> 
> 
> The same happens with MSVC.
> 
> 
> Do you think that the Warning is not much? 
> Well, you can use a compiler option
> 
> 
>     -Werror
> Or in this case....
>     -Werror-return-stack-address
> 
> 
> ...and... voilĂ , the problem is over, we now have errors instead of
> warnings.
> What is the advantage of Rust now? I think it's insignificant.
> 


I usually find myself defending C++ in most discussion but it's not
ideal (although from what I heard about article it is much less horrible
then presented).

#include <iostream>

class Test {
public:
    Test(int &i) : i(i) {}
    int get_i() const {
        i++
        return i;
    }
private:
    int &i;
};

Test foo(int i) {
    return Test(i);
}

int main() { 
    Test a = foo(1);
    Test b = foo(2);
    std::cout << a.get_i() << " " << b.get_i() << std::endl;
    return 0;
}

No warnings on -Wall -Wextra -O1. Of course -O1 and -O0 have different
results as we are happy to write somewhere on stack and in first case
the valgrind does not report any errors. Add more code, foo begin a bit
longer (or getting itself a reference) multiple files, Cheshire Cats...

Once you get any structure more complicated then tree you'll need to use
pointers. You can use std::shared_pointer as long as your structure is
DAG - once it isn't you are on your own. 'Rust' have similar problem -
but it has compile error instead of potentially a warning (potentially
as C++ needs to have 'sufficient' knowledge).

(Not mentioning the reference invalidation mentioned earlier)

Best regards


Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to