Optional library

Hi I ran the following code: const std::string i = "some string"; boost::optional<const std::string&> str = i; cout << get_optional_value_or(str, "some other string") ; It gives the output as : some string Which is absolutely fine. But when I run const std::string i = {}; boost::optional<const std::string&> str = i; cout << get_optional_value_or(str, "some other string") ; The output is an empty string. Shouldn't it give "some other string as output" because if I run the same code with integer it gives the second parameter of get_optional_value_or() as output?

On Sun, 19 Jan 2020 at 11:57, anshu khare via Boost <boost@lists.boost.org> wrote:
But when I run
const std::string i = {}; boost::optional<const std::string&> str = i; cout << get_optional_value_or(str, "some other string") ;
The output is an empty string. Shouldn't it give "some other string as output"
No, it should not.
From the documentation: "Class template optional is a wrapper for representing 'optional' (or 'nullable') objects who may not (yet) contain a valid value."
An empty string in `i` variable IS a valid value, so `str` is set with a valid value, Since it is valid value, get_optional_value_or returns that valid value and not the "some other string".
because if I run the same code with integer it gives the second parameter of get_optional_value_or() as output?
Are you sure? This is expected to print 0, the valid value of `i` and not the default 1: int i = {}; boost::optional<int> v = i; cout << get_optional_value_or(v, 1); Best regards, -- Mateusz Loskot, http://mateusz.loskot.net

When I run the following code : int i = {}; boost::optional<int> v = i; cout << get_optional_value_or(v, 1) I get a compilation error in the third line. On Sun, 19 Jan, 2020, 5:12 PM Mateusz Loskot via Boost, < boost@lists.boost.org> wrote:
On Sun, 19 Jan 2020 at 11:57, anshu khare via Boost <boost@lists.boost.org> wrote:
But when I run
const std::string i = {}; boost::optional<const std::string&> str = i; cout << get_optional_value_or(str, "some other string") ;
The output is an empty string. Shouldn't it give "some other string as output"
No, it should not.
From the documentation: "Class template optional is a wrapper for representing 'optional' (or 'nullable') objects who may not (yet) contain a valid value."
An empty string in `i` variable IS a valid value, so `str` is set with a valid value, Since it is valid value, get_optional_value_or returns that valid value and not the "some other string".
because if I run the same code with integer it gives the second parameter of get_optional_value_or() as output?
Are you sure?
This is expected to print 0, the valid value of `i` and not the default 1:
int i = {}; boost::optional<int> v = i; cout << get_optional_value_or(v, 1);
Best regards, -- Mateusz Loskot, http://mateusz.loskot.net
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

However when I run boost::optional<int> i= 5; cout << get_optional_value_or(i, 7) ; I get the output as 5. And when I run boost::optional<int> i= {}; cout << get_optional_value_or(i, 7) ; I get the output as 7. On Sun, 19 Jan, 2020, 5:29 PM anshu khare, <anshukhare1998@gmail.com> wrote:
When I run the following code :
int i = {}; boost::optional<int> v = i; cout << get_optional_value_or(v, 1)
I get a compilation error in the third line.
On Sun, 19 Jan, 2020, 5:12 PM Mateusz Loskot via Boost, < boost@lists.boost.org> wrote:
On Sun, 19 Jan 2020 at 11:57, anshu khare via Boost <boost@lists.boost.org> wrote:
But when I run
const std::string i = {}; boost::optional<const std::string&> str = i; cout << get_optional_value_or(str, "some other string") ;
The output is an empty string. Shouldn't it give "some other string as output"
No, it should not.
From the documentation: "Class template optional is a wrapper for representing 'optional' (or 'nullable') objects who may not (yet) contain a valid value."
An empty string in `i` variable IS a valid value, so `str` is set with a valid value, Since it is valid value, get_optional_value_or returns that valid value and not the "some other string".
because if I run the same code with integer it gives the second parameter of get_optional_value_or() as output?
Are you sure?
This is expected to print 0, the valid value of `i` and not the default 1:
int i = {}; boost::optional<int> v = i; cout << get_optional_value_or(v, 1);
Best regards, -- Mateusz Loskot, http://mateusz.loskot.net
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

This is a top-post reminding people to please not top-post: <https://www.boost.org/community/policy.html#quoting> On Sun, 19 Jan 2020 at 13:02, anshu khare via Boost <boost@lists.boost.org> wrote:
However when I run
boost::optional<int> i= 5; cout << get_optional_value_or(i, 7) ;
I get the output as 5.
And when I run
boost::optional<int> i= {}; cout << get_optional_value_or(i, 7) ;
I get the output as 7.
Correct! This is expected behaviour. Now, go back to your previous example and compare with the above. Hint: int i = {}; // zero boost::optional<int> v = i; vs boost::optional<int> i= {}; May I suggest you to read through the documentation of boost::optional as well as https://en.cppreference.com/w/cpp/utility/optional, before you waste more time on experiments to reverse-engineer what the library is supposed to do? Best regards, -- Mateusz Loskot, http://mateusz.loskot.net

First and foremost, please respect the rules and stop top-posting https://www.boost.org/community/policy.html#quoting> On Sun, 19 Jan 2020 at 12:59, anshu khare via Boost <boost@lists.boost.org> wrote:
When I run the following code :
int i = {}; boost::optional<int> v = i; cout << get_optional_value_or(v, 1)
I get a compilation error in the third line.
¯\_(ツ)_/¯ my crystal sphere has broken https://godbolt.org/z/u3yUgU #include <boost/optional.hpp> #include <iostream> using namespace std; int main() { { int i = {}; // zero boost::optional<int> v = i; cout << get_optional_value_or(v, 1) << endl; // prints 0 } { int i = {5}; boost::optional<int> v = i; cout << get_optional_value_or(v, 1) << endl; // prints 5 } } Best regards, -- Mateusz Loskot, http://mateusz.loskot.net
participants (2)
-
anshu khare
-
Mateusz Loskot