3 ms·
> returning a proxy object from operator[] instead of a reference. Oh yeah, maybe. But that would still require the ability to overload the dot operator, would
by duneroadrunner 10y ago
> returning a proxy object from operator[] instead of a reference.
Oh yeah, maybe. But that would still require the ability to overload the dot operator, wouldn't it? And how would you know when to deallocate the proxy object? And presumably there would be some run-time overhead. Hmm, I don't know if it wouldn't be more practical to create a static tool (or "precompiler") to automatically convert references to (safe) pointers (or iterators).
- zerofan 10y agoYes, the dot operator is a headache. As soon as you march down the road of a precompiler, you're off to building a new language. I think C++'s grammar is too much of a mess to just tweak the parse tree reliably. I suspect there really isn't a way to win at this - every workaround is partial and involves compromise.
- duneroadrunner 10y agoYou know, while C++ references are technically unsafe, there is TRegisteredRefWrapper<> [1]. It's a safe version of std::reference_wrapper. Which kind of acts like a reference. So, if you don't mind me using std::strings instead of doubles, your example could be rewritten like mse::mstd::vector<mse::TRegisteredObj<std::string>> data(10); data[0] = "some text"; mse::TRegisteredRefWrapper<std::string> dangling = data[0]; data.resize(100000); try { std::string crashing = dangling; } catch (...) { // expected exception (not a segfault) int q = 3; } Does that work for you? I'm not an expert on std::reference_wrapper, so I'm not sure when it can and cannot substitute for a reference. (Btw, if it's a little verbose for you, there are shorter aliases available. Just search for "shorter aliases" in the header files.) [1] https://github.com/duneroadrunner/SaferCPlusPlus#tregisteredrefwrapper https://github.com/duneroadrunner/SaferCPlusPlus#tregistered...