New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem with getLayerAs for const pointers #114
Comments
I think it's possible to fix this by casting away const, i.e. change lines 98-100 in Layer.hpp to template <typename T>
const T& getLayerAs() const {
return const_cast<Layer*>(this)->getLayerAs<T>();
} but would appreciate an expert opinion on that! |
Interesting 🤔 When I get a chance I'll have to try it on other platforms to see if I get the same results. However I'm not a fan of the |
Certainly would be do-able to create const overloads for each of the specialisations, if a little tedious. How about if it were swapped around - the specialisations are all for the const version? Then the non-const overload would cast to const (should always be safe), cast to the subclass, and then cast back to non-const (should also be safe because we know, within that same function, the pointer was to a non-const object in the first place) |
To be honest I'd prefer to do the work (it only has to be done once to be included in the library anyway) if it means the intended behaviour is explicit. After all it was my assumption based on implicit behaviour that all compilers would pick the correct specialisation which caused the bug in the first place 😉 I've made the changes and pushed them to the const_correctness branch - give it a try and see if it fixes it for you. If it does let me know and I'll merge it. Thanks! |
That certainly solved the problem I was having, 👍 |
Good enough, thanks! |
If I call
tmx::Layer::getLayerAs<tmx::something>()
on aconst tmx::Layer*
I get infinite recursion in this line:tmxlite/tmxlite/include/tmxlite/Layer.hpp
Lines 98 to 100 in 7872ea6
I believe that, if
this
is a const pointer, the overload shown above is selected ahead of the non-const version (line 91-92 of Layer.hpp) and it just calls itself without changing the type of any of the arguments. I suppose the intention was to call the non-const version which makes use of the template specializations in LayerGroup etc.Here is an example program that shows it (using the platform.tmx file from ParseTest)
The "return bool" business is just to stop the compiler from optimizing out the call to
getLayerAs
. Thenot_ok
call never returns, eventually fills the stack and crashes.Hope that makes sense!
I'm using C++ Apple Clang 12.0.0
The text was updated successfully, but these errors were encountered: