Boost logo

Boost Users :

Subject: [Boost-users] [Optional][Variant] Different behavior vs std c++17
From: dariomt_at_[hidden]
Date: 2018-03-14 14:54:25


Hi list,

I'm observing different behavior in boost::optional and boost::variant
compared to their std c++17 versions. Using Boost 1.64

The following example fails to compile with -DUSE_BOOST using either gcc
7.3 or clang 6.0

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

struct A {};
struct B {};
struct C {};

#ifdef USE_BOOST
using V = boost::variant<A,B,C>;
using OV = boost::optional<V>;
#else
using V = std::variant<A,B,C>;
using OV = std::optional<V>;
#endif

OV f(B b)
{
    return b;
}

Is this difference in behavior expected for such a simple example?

Is it possible to improve Boost to make this example compile?

Thanks in advance!

PS: the Boost only version works in VS2015, not sure why...

PPS: Try it in godbolt! https://godbolt.org/g/n4Vbr9

gcc 7.3 says
<source>: In function 'OV f(B)':
<source>:20:12: error: could not convert 'b' from 'B' to 'OV {aka
boost::optional<boost::variant<A, B, C> >}'

clang 6.0.0 says
<source>:20:12: error: no viable conversion from returned value of type 'B'
to function return type 'OV' (aka 'optional<variant<A, B, C> >')
    return b;
           ^
/opt/compiler-explorer/libs/boost_1_64_0/boost/optional/optional.hpp:796:5:
note: candidate constructor not viable: no known conversion from 'B' to
'boost::none_t' for 1st argument
    optional( none_t none_ ) BOOST_NOEXCEPT : base(none_) {}
    ^
/opt/compiler-explorer/libs/boost_1_64_0/boost/optional/optional.hpp:800:5:
note: candidate constructor not viable: no known conversion from 'B' to
'boost::optional<boost::variant<A, B, C> >::argument_type' (aka 'const
boost::variant<A, B, C> &') for 1st argument
    optional ( argument_type val ) : base(val) {}
    ^
/opt/compiler-explorer/libs/boost_1_64_0/boost/optional/optional.hpp:805:5:
note: candidate constructor not viable: no known conversion from 'B' to
'boost::optional<boost::variant<A, B, C> >::rval_reference_type' (aka
'boost::variant<A, B, C> &&') for 1st argument
    optional ( rval_reference_type val ) : base( boost::forward<T>(val) )
    ^
/opt/compiler-explorer/libs/boost_1_64_0/boost/optional/optional.hpp:877:5:
note: candidate constructor not viable: no known conversion from 'B' to
'const boost::optional<boost::variant<A, B, C> > &' for 1st argument
    optional ( optional const& rhs ) : base( static_cast<base const&>(rhs)
) {}
    ^
/opt/compiler-explorer/libs/boost_1_64_0/boost/optional/optional.hpp:882:2:
note: candidate constructor not viable: no known conversion from 'B' to
'boost::optional<boost::variant<A, B, C> > &&' for 1st argument
        optional ( optional && rhs )



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net