Skip to content

optional<bool>(optional<bool>) constructor is broken in 1.91 #1143

@eltoder

Description

@eltoder

It appears that changes to boost::optional in 1.91 introduced a bug in optional<bool>(optional<bool>) constructor similar to the one from LWG issue 3836. That is, instead of copying the source optional, it converts it to a bool using its operator bool and stores that result in the new optional. This results in an optional that always has a value, which is equal to whether the source optional had a value.

Test case:

#include <optional>
#include <iostream>
#include <boost/optional.hpp>

using WHICH::optional;

template <class T>
std::ostream& print(std::ostream& s, const optional<T>& opt) {
    return opt ? s << *opt : s << "--";
}

int main() {
    optional<bool> a;
    optional<bool> b(a);
    optional<bool> c(b);
    print(std::cout, a) << " ";
    print(std::cout, b) << " ";
    print(std::cout, c) << std::endl;
    return 0;
}

Compiling with std::optional gives the expected behavior:

$ g++ -DWHICH=std t.cc && ./a.out
-- -- --

Compiling with boost::optional from 1.91 shows the bug:

$ g++ -DWHICH=boost t.cc && ./a.out
-- 0 1

Earlier versions of boost worked the same way as std::optional.

Tested on linux with g++ version 13.3.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions