ispringtech / FastSignals

Easy to use, fast and lightweight C++17 signals and slots library, drop-in replacement for the Boost.Signals2
MIT License
25 stars 10 forks source link

Unspecified behavior when comparing pointers in packed_function::is_buffer_allocated() #11

Closed alexey-malov closed 5 years ago

alexey-malov commented 5 years ago

https://github.com/ispringteam/FastSignals/blob/5c05334245ba0391994788ce8a9cca32ac335fa9/libfastsignals/src/function_detail.cpp#L82-L88

Comparing pointers using <, >, <=, >= operators in C++ is safe only when both pointers point to the elements of the same array (or the memory location past the last element of that array). When pointers point to items of the different arrays, or to different objects, the result is unspecified.

To compare arbitrary pointers it is necessary to use template specializations of std::greater, std::less, std::greater_equal, std::less_equal. In this case they will yield a total order.

§ 20.8.5/8: "For templates greater, less, greater_equal, and less_equal, the specializations for any pointer type yield a total order, even if the built-in operators <, >, <=, >= do not."

Warboss-rus commented 5 years ago

std::launder solution is still not portable. More on this issue from Raymond Chen: https://blogs.msdn.microsoft.com/oldnewthing/20170927-00/?p=97095