Skip to content

Instantly share code, notes, and snippets.

@hutorny
Created November 15, 2016 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hutorny/69a568de22aa59e2c64957fd0cacd52e to your computer and use it in GitHub Desktop.
Save hutorny/69a568de22aa59e2c64957fd0cacd52e to your computer and use it in GitHub Desktop.
/* container_of, inspired by AndrewTsao */
template <class T, typename M>
constexpr T* container_of(M *m, const M T::*p) noexcept {
return reinterpret_cast<T*>(reinterpret_cast<char*>(m)
- reinterpret_cast<const ptrdiff_t>(
&(static_cast<const T *>(nullptr)->*p)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment