c++c++17stdoptionalstdany

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
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.