boost-optional

How should one use std::optional?

我的梦境 提交于 2019-12-03 18:22:23
问题 I'm reading the documentation of std::experimental::optional and I have a good idea about what it does, but I don't understand when I should use it or how I should use it. The site doesn't contain any examples as of yet which leaves it harder for me to grasp the true concept of this object. When is std::optional a good choice to use, and how does it compensate for what was not found in the previous Standard (C++11). 回答1: The simplest example I can think of: std::optional<int> try_parse_int

Viewing a raw pointer as a range in range-based for-loop

风格不统一 提交于 2019-12-01 20:18:18
How can I make a raw pointer behave like a range, for a for-range loop syntax. double five = 5; double* dptr = &five; for(int& d : dptr) std::cout << d << std::endl;// will not execute if the pointer is null Motivation: It is now vox populi that an boost::optional (future std::optional ) value can be viewed as a range and therefore used in a for range loop http://faithandbrave.hateblo.jp/entry/2015/01/29/173613 . When I rewrote my own simplified version of it: namespace boost { template <class Optional> decltype(auto) begin(Optional& opt) noexcept{ return opt?&*opt:nullptr; } template <class

Is it possible to move a boost::optional?

拈花ヽ惹草 提交于 2019-12-01 02:42:22
I've been trying to define a defaulted move constructor in a class with a boost::optional member variable. #include <boost/optional.hpp> #include <utility> #include <vector> struct bar {std::vector<int> vec;}; struct foo { foo() = default; foo(foo&&) = default; boost::optional<bar> hello; }; int main() { foo a; foo b(std::move(a)); } My compiler supports both move semantics and defaulted move constructors, but I cannot get this to work. % clang++ foo.cc -std=c++11 -stdlib=libc++ foo.cc:15:7: error: call to deleted constructor of 'foo' foo b(std::move(a)); ^ ~~~~~~~~~~~~ foo.cc:9:3: note:

Implementing boost::optional in c++11

﹥>﹥吖頭↗ 提交于 2019-11-30 14:04:42
I am experimenting with implementing boost::optional like data structure using c++11 features. Here is what I have so far : template<typename T> struct maybe { bool valid; union { T value; }; maybe() : valid(false) {} maybe(const T& _v) { valid = true; new (&value) T(_v); } maybe(const maybe& other) { if (other.valid) { valid = true; new (&value) T(other.value); } else valid = false; } ~maybe() { if (valid) value.~T(); } bool is_valid() { return valid; } operator T&() { if (valid) return value; throw std::bad_exception(); } }; I make use of the unrestricted union feature to create a properly

boost::optional alternative in C++ Standard Library

隐身守侯 提交于 2019-11-30 11:07:41
I'm trying to get my program working without boost usage, but can't find an alternative of some useful patterns. Namely, I can't find boost::optional -likewise pattern in the standard library. Is there some standard alternative for boost::optional (C++11 or somewhere else)? Short answer: No. Long answer: Roll your own according to the boost spec. The documentation is quite exhaustive and the code isn't that complex, but this still requires above average C++ skills. To update this answer: C++14 unfortunately did not ship with std::optional . The current proposal (Revision 5) is N3793 and it is

std::optional specialization for reference types

陌路散爱 提交于 2019-11-30 07:57:14
问题 Why std::optional ( std::experimental::optional in libc++ at the moment) does not have specialization for reference types (compared with boost::optional )? I think it would be very useful option. Is there some object with reference to maybe already existing object semantics in STL ? 回答1: When n3406 (revision #2 of the proposal) was discussed, some committee members were uncomfortable with optional references. In n3527 (revision #3), the authors decided to make optional references an auxiliary

Implementing boost::optional in c++11

情到浓时终转凉″ 提交于 2019-11-29 19:49:11
问题 I am experimenting with implementing boost::optional like data structure using c++11 features. Here is what I have so far : template<typename T> struct maybe { bool valid; union { T value; }; maybe() : valid(false) {} maybe(const T& _v) { valid = true; new (&value) T(_v); } maybe(const maybe& other) { if (other.valid) { valid = true; new (&value) T(other.value); } else valid = false; } ~maybe() { if (valid) value.~T(); } bool is_valid() { return valid; } operator T&() { if (valid) return

How should one use std::optional?

北城以北 提交于 2019-11-29 18:44:15
I'm reading the documentation of std::experimental::optional and I have a good idea about what it does, but I don't understand when I should use it or how I should use it. The site doesn't contain any examples as of yet which leaves it harder for me to grasp the true concept of this object. When is std::optional a good choice to use, and how does it compensate for what was not found in the previous Standard (C++11). Timothy Shields The simplest example I can think of: std::optional<int> try_parse_int(std::string s) { //try to parse an int from the given string, //and return "nothing" if you

What is the rationale for boost::none_t implementation?

流过昼夜 提交于 2019-11-29 16:51:49
问题 Boost.Optional uses a dummy type to allow constructing uninitialized instances of boost::optional<T> . This type is called none_t , and an instance none is already defined in a header for convenience, allowing us to write code such as the following: boost::optional<int> uninitialized(boost::none); Looking at the definition of none_t , I noticed that it is in fact a typedef corresponding to a pointer-to-member to some dummy struct: namespace boost { namespace detail { struct none_helper{}; }

std::optional specialization for reference types

偶尔善良 提交于 2019-11-29 05:29:59
Why std::optional ( std::experimental::optional in libc++ at the moment) does not have specialization for reference types (compared with boost::optional )? I think it would be very useful option. Is there some object with reference to maybe already existing object semantics in STL ? When n3406 (revision #2 of the proposal) was discussed, some committee members were uncomfortable with optional references. In n3527 (revision #3), the authors decided to make optional references an auxiliary proposal, to increase the chances of getting optional values approved and put into what became C++14. While