5 ms·
Small thing, but some of the examples have some pretty bad undefined behavior bugs. For instance: Matrix& operator*( const double scalar,
by overgard 11y ago
Small thing, but some of the examples have some pretty bad undefined behavior bugs. For instance:
Matrix& operator*( const double scalar,
const Matrix& rhs)
{
Matrix result(rhs);
result *= scalar;
return result;
}
(Returning a reference to a stack variable is undefined behavior, but realistically you're probably either looking at a hard crash or a really hard to track down bug)
- santaclaus 11y agoI wonder why they didn't simply nuke the reference and get move semantics? Or better yet, use expression templates.
- codeofthedamned 11y agoI have corrected the code by removing the reference. The way the code is now structured will allow for the RVO (Return-value optimization) to occur, so the new instance will be constructed in the location of the receiving object. Move semantics would not apply in this case, it would actually suffer from a similar problem as the version that returned a reference, except the program would not crash. Instead the return value would contain garbage. (edit: added a link to details on how to use move semantics) http://codeofthedamned.com/index.php/c-r-value-references http://codeofthedamned.com/index.php/c-r-value-references I did not use expression templates, or try to optimize any of the implementation because my intent is to demonstrate "how" to get started with the math. I also wanted to make the code accessible to programmers that do not use C++. For example, someone trying to learn WebGL.
- codeofthedamned 11y agoThanks for pointing out my mistake, I have corrected the code to simply return from the stack.
- jheriko 11y agothat should be a compile error, surely? 'returning a reference or address of a temporary' or similar... i guess warnings as errors and having warnings to the max has spoiled me.