Add C++11 unrestricted union to the C++ style guide. Review: https://reviews.apache.org/r/33572
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/30113086 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/30113086 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/30113086 Branch: refs/heads/master Commit: 30113086a7cf3c91c200812a6785e9b97a211bb1 Parents: 765b6c8 Author: Benjamin Hindman <[email protected]> Authored: Sun Apr 26 13:16:52 2015 -0700 Committer: Benjamin Hindman <[email protected]> Committed: Sat May 2 10:28:21 2015 -0700 ---------------------------------------------------------------------- docs/mesos-c++-style-guide.md | 4 ++++ 1 file changed, 4 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/30113086/docs/mesos-c++-style-guide.md ---------------------------------------------------------------------- diff --git a/docs/mesos-c++-style-guide.md b/docs/mesos-c++-style-guide.md index 5437bfd..0bdca80 100644 --- a/docs/mesos-c++-style-guide.md +++ b/docs/mesos-c++-style-guide.md @@ -404,3 +404,7 @@ Try<Owned<LocalAuthorizer>> authorizer = LocalAuthorizer::create(); ...; }; ``` + +* Unrestricted Union. + + Like the pre-existing `union`, we can overlap storage allocation for objects that never exist simultaneously. However, with C++11 we are no longer *restricted to having only non-POD types in unions*. Adding non-POD types to unions complicates things, however, because we need to make sure to properly call constructors and destructors. Therefore, only use unrestricted unions (i.e., unions with non-POD types) when the union has only a single field. What does this buy us? Now we can avoid dynamic memory allocations for "container" like types, e.g., `Option`, `Try`, `Result`, etc. In effect, we treat the union like a dynamic allocation, calling *placement new*, `new (&t) T(...)` anyplace we would have just called `new T(...)` and the destructor `t.~T()` anyplace we would have called `delete t`.
