为什么 Boost Variant 使用模板构造函数而不是 boost::beast::websocket::stream 的移动构造函数?

Why does Boost Variant use the template constructor instead of the move constructor for boost::beast::websocket::stream?

我正在尝试将 boost::beast::websocket::stream<T>(对于 2 个特定的 T)包装在 boost::variant 中以允许处理 TLS([T = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>])和非 TLS([T = boost::asio::ip::tcp::socket]) websockets 相同。我卡在编译失败了。

我能想到的最简单的失败示例是:

#include <boost/asio/ssl.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/variant.hpp>

using tcp = boost::asio::ip::tcp;
namespace ws = boost::beast::websocket;
namespace ssl = boost::asio::ssl;

using base_ws = boost::variant<
    ws::stream<tcp::socket>, ws::stream<ssl::stream<tcp::socket>>>;

class test
{
 public:
    static void init( tcp::socket &&  socket )
    {
        ssl::context  ctx{ ssl::context::tlsv12_server };
        ws::stream<ssl::stream<tcp::socket>>  s{ std::move( socket ), ctx };
        base_ws{ std::move( s ) };
    }
};

int main()
{}

编译失败并出现错误:

In file included from /server/src/ws_server.cpp:9:
In file included from /include/boost/beast/websocket.hpp:18:
In file included from /include/boost/beast/websocket/stream.hpp:3455:
/include/boost/beast/websocket/impl/stream.ipp:47:7: error: no matching constructor for initialization of 'boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >'
    : stream_(std::forward<Args>(args)...)
      ^       ~~~~~~~~~~~~~~~~~~~~~~~~
/include/boost/variant/detail/initializer.hpp:122:27: note: in instantiation of function template specialization 'boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > >::stream<boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > > >' requested here
                new(dest) value_T( boost::detail::variant::move(operand) );
                          ^
/include/boost/variant/variant.hpp:1687:28: note: in instantiation of member function 'boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::initializer_root, mpl_::int_<0> >, boost::mpl::l_iter<boost::mpl::list2<boost::beast::websocket::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >, boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > > > > >::initializer_node, mpl_::int_<1> >, boost::mpl::l_iter<boost::mpl::list1<boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > > > > >::initializer_node::initialize' requested here
              initializer::initialize(
                           ^
/include/boost/variant/variant.hpp:1858:9: note: in instantiation of function template specialization 'boost::variant<boost::beast::websocket::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >, boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > > >::convert_construct<boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > > >' requested here
        convert_construct( detail::variant::move(operand), 1L);
        ^
/server/src/ws_server.cpp:372:20: note: in instantiation of function template specialization 'boost::variant<boost::beast::websocket::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >, boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > > >::variant<boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > > >' requested here
            return base_ws( std::move( s ) );
                   ^
/include/boost/asio/ssl/stream.hpp:64:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'boost::beast::websocket::stream<boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > >' to 'const boost::asio::ssl::stream<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >' for 1st argument
class stream :
      ^
/include/boost/asio/ssl/stream.hpp:98:3: note: candidate constructor template not viable: requires 2 arguments, but 1 was provided
  stream(Arg&& arg, context& ctx)
  ^

我会提供 godbolt 但它会超时,而 coliru 不提供 boost/asio/ssl.hpp.

所需的 openssl

根据 hereboost::variant 应该接受 T && 并使用它在内部实例化 T

根据 hereboost::beast::websocket::stream 确实有移动构造函数。

移动构造函数更具体,那么为什么编译器选择第一个注释中指示的可变参数构造函数 (stream<...>)?

boost::beast::websocket::stream<
  boost::asio::ssl::stream<
    boost::asio::basic_stream_socket<boost::asio::ip::tcp>
  >
>::stream<
  boost::beast::websocket::stream<
    boost::asio::ssl::stream<
      boost::asio::basic_stream_socket<boost::asio::ip::tcp>
    >
  >
>

我正在使用 Boost 1.66.0 和 Clang 7。

我忽略的关键信息在boost::beast::websocket::stream::stream(stream&&)的描述中:

If NextLayer is move constructible, this function will move-construct a new stream from the existing stream.

原来boost::asio::ssl::stream是不可移动构造的,所以没有生成stream的默认移动构造函数。在 chriskohlhoff/asio#124 which references the boost bugtracker.

中使 ssl::stream 移动可构造是一个悬而未决的问题

解决该问题的方法是使用 ssl::stream 的可移动构造实现,就像 Beast 示例 here 中提供的那样,它看起来可能在 [=16] =] 在更高版本的 Beast 中(不在 1.67.0 中)。

advanced-server-flex 示例展示了如何使用处理 SSL 和纯 websocket 会话的代码构建服务器,而无需求助于变体:https://github.com/boostorg/beast/blob/c2ecba968c06a22a61c67b8887f5b477bb32a99a/example/advanced/server-flex/advanced_server_flex.cpp#L227