Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does std::optional work under the hood?

I just learned about the std::optional feature in c++ 17 along with a few other very useful features...

but there is a few things that I don't understand about std::optional that I would like someone to explain them to me:

first of all as far as I know in std::optional the return value can either be the specified type or it can be nothing like this :

std::optional<std::string> getName()
{
    if(person.hasName())
    {
        return person.name;
    }
    else 
    {
        return {};
    }
}

how does return {} returns nothing ? like for example if I was to make a similar class that return either the specified value or nothing, how do I make my own class so return {} is valid ? I'm I misunderstanding something here ?

my second question is that when you want to check the return value you can either do :

int main()
{
    std::optional<std::string> name = getName();
    if(name.has_value())  // check if the name is valid using the has_value function
    {
        ...
    }
}

or i can also do :


int main()
{
    std::optional<std::string> name = getName();
    if(name)  // check if the name is valid only using the variable name ???
    {
        ...
    }
}

I'm really confused about this how could a variable name return a boolean ? not like the constructor of an object can return anything, so how is this possible ?

again let's say I want to make my own class that is kind of similar to std::optional how do I make it so an instance of my class can be used as a boolean ?

I would really appreciate answers that adress my questions and not something related to when to use std::optional or why I shouldn't make my own class that does the same thing etc...

thanks!

like image 384
Ronald joe Avatar asked Nov 21 '25 06:11

Ronald joe


1 Answers

return {};

will simply call the default constructor of the class.

By giving the class a conversion operator to bool it can be implicitly converted to a bool when needed.

It would look something along the lines of

template <typename T>
class optional {
    public:
    optional() {}
    optional(T t) : has_value(true), value(std::move(t)) {}

    operator bool() {
        return has_value;
    }
    
    private:
    bool has_value = false;
    T value;
}

Very simplified, missing assignement operators and more.

like image 143
super Avatar answered Nov 23 '25 20:11

super



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!