Skip to content

Problem using BOOST_TEST(a==b) to compare boost::variant in gcc12 in C++20 mode #484

@dmenendez-gruposantander

Description

I'm using Boost 1.90, and comparing two variants within a BOOST_TEST macro fails.

I believe the C++20 comparison rewiriting rules is creating problems with this compiler.

This is the minimal repro I distilled in godobolt: https://godbolt.org/z/8q1ja37G6

#include <boost/variant.hpp>
#include <boost/test/included/unit_test.hpp>
#include <iosfwd>

struct A {};
struct B {};

bool operator== (const A& lhs, const A& rhs);
bool operator== (const B& lhs, const B& rhs);

std::ostream operator<< (std::ostream& os, const A& a);
std::ostream operator<< (std::ostream& os, const B& b);

using C = boost::variant<A,B>;

bool f(const A& a1, const A& a2)
{
    return a1 == a2;
}

void g(const A& a1, const A& a2)
{
    BOOST_TEST(a1 == a2);
}

bool f(const C& c1, const C& c2)
{
    return c1 == c2;
}

void g(const C& c1, const C& c2)
{
    BOOST_TEST(c1 == c2);
}

The edited error message is:

error: return type of 'void boost::variant<T0, TN>::operator==(const U&) const [with U = boost::test_tools::assertion::value_expr<const boost::variant<A, B>&>; T0_ = A; TN = {B}]' is not 'bool'
        BOOST_TEST(c1 == c2);
note: used as rewritten candidate for comparison of 'boost::test_tools::assertion::value_expr<const boost::variant<A, B>&>' and 'const C' {aka 'const boost::variant<A, B>'}
/app/boost/include/boost/variant/variant.hpp: In instantiation of 'void boost::variant<T0, TN>::operator==(const U&) const [with U = boost::test_tools::assertion::value_expr<const boost::variant<A, B>&>; T0_ = A; TN = {B}]':
/app/boost/include/boost/variant/variant.hpp:2086:9: error: static assertion failed: false && sizeof(U)
 2086 |         BOOST_STATIC_ASSERT( false && sizeof(U) );

The workaround is to use extra parenthesis in the macro, to avoid the smart decomposition that BOOST_TEST does, but then I loose that smart decomposition.
Also, as seen in the godbolt link, gcc15.2 is able to compile the code correctly, but I cannot update compiler so easily :(

Thanks in advance

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