Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Interaction between std::optional<std::any> and has_value()

For debugging purposes, I was writing a function which iterates over a vector of optional variables of any type to check which ones were initialized, but the check for has_value() on all of them is returning true, despite no value having ever been assigned to some of them. I’d appreciate any help pointing out what I’m misunderstanding, as I’m new to C++. The code is below. Note that when the commented line is uncommented, the if statement picks up that the variable had no value.

#include <iostream>
#include <optional>
#include <any>

bool SimpleCheck(std::vector<std::optional<std::any>> toCheck)
{
    bool res = false;
    for (int i = 0; i < toCheck.size(); ++i)
    {
        // toCheck[i] = std::nullopt;
        if (!toCheck[i].has_value())
        {
            std::cout << "item at index " << i << " had no value\n";
            res = true;
        }
    }
    return res;
}

int main() 
{
    std::optional<int> i = 5;
    std::optional<std::string> str;
    std::optional<double> doub = std::nullopt;
    bool check = SimpleCheck({i, str, doub});
    std::cout << check << "\n";
    return 0;
}

My expected output is:

item at index 1 had no value

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

item at index 2 had no value

1

The actual output is:

0

If the commented line is uncommented, the output is:

item at index 0 had no value

item at index 1 had no value

item at index 2 had no value

1

>Solution :

With

std::optional<double> doub = std::nullopt;
std::optional<std::any> a = doub; // it is not a copy constructor

a is non empty, but its any is an empty std::optional.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading