使用 boost 数据结构和 nghttp2 的 C++ 程序未编译

C++ program using boost data structure and nghttp2 not compiling

我有一个使用 nghttp2. In this server I am trying to use boost's circular_buffer 数据结构创建的 http 服务器。

这是我试图在 Ubuntu 20.04.

上编译的 C++ 程序
#include <iostream>
#include <nghttp2/asio_http2_server.h>
#include <boost/circular_buffer.hpp>

using namespace nghttp2::asio_http2;
using namespace nghttp2::asio_http2::server;

int main(int argc, char *argv[]) {
  boost::circular_buffer<request> buffer(3);
  
  boost::system::error_code ec;
  http2 server;

  server.handle("/", [&buffer](const request &req, const response &res) {
   
   buffer.push_back(req);
  });

  if (server.listen_and_serve(ec, "localhost", "3000")) {
    std::cerr << "error: " << ec.message() << std::endl;
  }
}

我用这个命令编译这段代码:

g++ -std=c++14 server.cc -o server -lnghttp2_asio -lboost_system -lcrypto -lpthread -lssl -lboost_thread

当我编译这个时,我得到以下错误输出

In file included from /usr/include/x86_64-linux-gnu/c++/9/bits/c++allocator.h:33,
                 from /usr/include/c++/9/bits/allocator.h:46,
                 from /usr/include/c++/9/string:41,
                 from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from http-server.cc:1:
/usr/include/c++/9/ext/new_allocator.h: In instantiation of 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = nghttp2::asio_http2::server::request; _Args = {const nghttp2::asio_http2::server::request&}; _Tp = nghttp2::asio_http2::server::request]':
/usr/include/c++/9/bits/alloc_traits.h:482:2:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = nghttp2::asio_http2::server::request; _Args = {const nghttp2::asio_http2::server::request&}; _Tp = nghttp2::asio_http2::server::request; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<nghttp2::asio_http2::server::request>]'
/usr/include/boost/circular_buffer/base.hpp:1427:59:   required from 'void boost::circular_buffer<T, Alloc>::push_back_impl(ValT) [with ValT = const nghttp2::asio_http2::server::request&; T = nghttp2::asio_http2::server::request; Alloc = std::allocator<nghttp2::asio_http2::server::request>]'
/usr/include/boost/circular_buffer/base.hpp:1474:9:   required from 'void boost::circular_buffer<T, Alloc>::push_back(boost::circular_buffer<T, Alloc>::param_value_type) [with T = nghttp2::asio_http2::server::request; Alloc = std::allocator<nghttp2::asio_http2::server::request>; boost::circular_buffer<T, Alloc>::param_value_type = const nghttp2::asio_http2::server::request&; boost::circular_buffer<T, Alloc>::value_type = nghttp2::asio_http2::server::request]'
http-server.cc:16:24:   required from here
/usr/include/c++/9/ext/new_allocator.h:145:20: error: use of deleted function 'nghttp2::asio_http2::server::request::request(const nghttp2::asio_http2::server::request&)'
  145 |  noexcept(noexcept(::new((void *)__p)
      |                    ^~~~~~~~~~~~~~~~~~
  146 |        _Up(std::forward<_Args>(__args)...)))
      |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from http-server.cc:2:
/usr/local/include/nghttp2/asio_http2_server.h:39:7: note: 'nghttp2::asio_http2::server::request::request(const nghttp2::asio_http2::server::request&)' is implicitly deleted because the default definition would be ill-formed:
   39 | class request {
      |       ^~~~~~~
/usr/local/include/nghttp2/asio_http2_server.h:39:7: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = nghttp2::asio_http2::server::request_impl; _Dp = std::default_delete<nghttp2::asio_http2::server::request_impl>]'
In file included from /usr/include/c++/9/memory:80,
                 from /usr/local/include/nghttp2/asio_http2.h:29,
                 from /usr/local/include/nghttp2/asio_http2_server.h:28,
                 from http-server.cc:2:
/usr/include/c++/9/bits/unique_ptr.h:414:7: note: declared here
  414 |       unique_ptr(const unique_ptr&) = delete;
      |       ^~~~~~~~~~
In file included from /usr/include/boost/circular_buffer.hpp:58,
                 from http-server.cc:3:
/usr/include/boost/circular_buffer/base.hpp: In instantiation of 'void boost::circular_buffer<T, Alloc>::replace(boost::circular_buffer<T, Alloc>::pointer, boost::circular_buffer<T, Alloc>::param_value_type) [with T = nghttp2::asio_http2::server::request; Alloc = std::allocator<nghttp2::asio_http2::server::request>; boost::circular_buffer<T, Alloc>::pointer = nghttp2::asio_http2::server::request*; boost::circular_buffer<T, Alloc>::param_value_type = const nghttp2::asio_http2::server::request&; boost::circular_buffer<T, Alloc>::value_type = nghttp2::asio_http2::server::request]':
/usr/include/boost/circular_buffer/base.hpp:1423:13:   required from 'void boost::circular_buffer<T, Alloc>::push_back_impl(ValT) [with ValT = const nghttp2::asio_http2::server::request&; T = nghttp2::asio_http2::server::request; Alloc = std::allocator<nghttp2::asio_http2::server::request>]'
/usr/include/boost/circular_buffer/base.hpp:1474:9:   required from 'void boost::circular_buffer<T, Alloc>::push_back(boost::circular_buffer<T, Alloc>::param_value_type) [with T = nghttp2::asio_http2::server::request; Alloc = std::allocator<nghttp2::asio_http2::server::request>; boost::circular_buffer<T, Alloc>::param_value_type = const nghttp2::asio_http2::server::request&; boost::circular_buffer<T, Alloc>::value_type = nghttp2::asio_http2::server::request]'
http-server.cc:16:24:   required from here
/usr/include/boost/circular_buffer/base.hpp:2413:14: error: use of deleted function 'nghttp2::asio_http2::server::request& nghttp2::asio_http2::server::request::operator=(const nghttp2::asio_http2::server::request&)'
 2413 |         *pos = item;
      |         ~~~~~^~~~~~
In file included from http-server.cc:2:
/usr/local/include/nghttp2/asio_http2_server.h:39:7: note: 'nghttp2::asio_http2::server::request& nghttp2::asio_http2::server::request::operator=(const nghttp2::asio_http2::server::request&)' is implicitly deleted because the default definition would be ill-formed:
   39 | class request {
      |       ^~~~~~~
/usr/local/include/nghttp2/asio_http2_server.h:39:7: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = nghttp2::asio_http2::server::request_impl; _Dp = std::default_delete<nghttp2::asio_http2::server::request_impl>]'
In file included from /usr/include/c++/9/memory:80,
                 from /usr/local/include/nghttp2/asio_http2.h:29,
                 from /usr/local/include/nghttp2/asio_http2_server.h:28,
                 from http-server.cc:2:
/usr/include/c++/9/bits/unique_ptr.h:415:19: note: declared here
  415 |       unique_ptr& operator=(const unique_ptr&) = delete;
      |                   ^~~~~~~~
 

第一条错误消息的要点是 request(const request&)(a.k.a。复制构造函数)被删除。 request class 不可复制。错误消息继续解释为什么 request 不可复制,但这是切线信息,除非您打算修改正在使用的库。

由于 request 是(隐式)不可复制的,因此您的 lambda 不允许将其 req 参数的副本推送到容器中(例如,放入 boost::circular_buffer,正如你正在尝试的那样)。你必须想出另一种方法。

在其他情况下,解决方案可能是 移动 而不是复制到容器中。但是,库指定的回调签名要求 req 参数是 const 引用,因此不允许从 req 移动。

(有些人会错误地尝试的一种方法是让 lambda 在容器中存储指向 req 参数的指针。不要这样做, 因为那里不能保证在当前对 lambda 的调用完成后指针将有效。)

看来 req 的意图是完全由处理程序处理。一个语法上有效的选项是从 req 中提取必要的数据并将该数据的副本存储在您自己定义的结构中。这是否可取超出了语法问题的范围;它进入了 nghttp2 库的预期用途(我不熟悉的东西)。