{"id":87,"date":"2008-11-14T19:34:07","date_gmt":"2008-11-15T02:34:07","guid":{"rendered":"http:\/\/www.venzon.org\/?p=87"},"modified":"2008-11-14T19:34:29","modified_gmt":"2008-11-15T02:34:29","slug":"c-optional-return-values","status":"publish","type":"post","link":"http:\/\/www.venzon.org\/?p=87","title":{"rendered":"C++ Optional Return Values"},"content":{"rendered":"<p>After reading <a href=\"http:\/\/nicollet.net\/blog\/cpp\/24-an-introduction-to-c-pointers\">this article<\/a> and being inspired, I decided to write a simple &#8220;optional&#8221; class that&#8217;s similar to <a href=\"http:\/\/www.boost.org\/doc\/libs\/1_36_0\/libs\/optional\/doc\/html\/index.html\">boost::optional<\/a> but lighter weight.\u00c2\u00a0 The idea is that often I write functions which should return a value but which might not have       a value to return &#8212; and for those cases, I should be able to specify that the return type is optional.\u00c2\u00a0 In the calling code, I should have the ability to check that yes, a value was returned, and get the return value.\u00c2\u00a0 Here&#8217;s the entire content of the class:<br \/>\n<!--more--><\/p>\n<pre lang=\"cpp\">template <typename T>\r\nclass optional\r\n{\r\n\tprivate:\r\n\t\tT value;\r\n\t\tbool value_valid;\r\n\t\t\r\n\t\tbool is_initialized() const {return value_valid;}\r\n\t\t\r\n\tpublic:\r\n\t\toptional() : value(T()), value_valid(false) {}\r\n\t\toptional(T newvalue) : value(newvalue), value_valid(true) {}\r\n\t\t\r\n\t\toperator bool() const {return is_initialized();}\r\n\t\t\r\n\t\tT get() {assert(is_initialized());return value;}\r\n\t\tT get_or_default(T thedefault) {return is_initialized()?get():thedefault;}\r\n};<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>After reading this article and being inspired, I decided to write a simple &#8220;optional&#8221; class that&#8217;s similar to boost::optional but lighter weight.\u00c2\u00a0 The idea is that often I write functions which should return a value but which might not have a value to return &#8212; and for those cases, I should be able to specify [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-87","post","type-post","status-publish","format-standard","hentry","category-prototyping"],"_links":{"self":[{"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/posts\/87","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.venzon.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=87"}],"version-history":[{"count":2,"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":89,"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions\/89"}],"wp:attachment":[{"href":"http:\/\/www.venzon.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.venzon.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.venzon.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}