Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. // Offset may vary for different Ts
  2. template <typename T>
  3. std::ptrdiff_t list_node_payload_offset() {
  4. std::list<T> lst;
  5. lst.push_back(T{});
  6. auto lst_begin = lst.begin();
  7. return reinterpret_cast<uint8_t*>(std::addressof(lst.front())) -
  8. *reinterpret_cast<uint8_t**>(std::addressof(lst_begin));
  9. }
  10.  
  11. #pragma GCC optimize "no-strict-aliasing"
  12. template <typename T>
  13. typename std::list<T>::iterator to_list_iterator(T& t) {
  14. static const auto offset = list_node_payload_offest<T>();
  15. auto node_address = reinterpret_cast<uint8_t*>(std::addressof(t)) - offset;
  16. return *reinterpret_cast<typename std::list<T>::iterator*>(&iter_value);
  17. }
  18. #pragma GCC optimize "strict-aliasing"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement