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

Posted

技术标签:

【中文标题】使用 boost 数据结构和 nghttp2 的 C++ 程序未编译【英文标题】:C++ program using boost data structure and nghttp2 not compiling 【发布时间】:2021-10-04 15:35:08 【问题描述】:

我有一个使用nghttp2 创建的http 服务器。在此服务器中,我尝试使用 boost 的 circular_buffer 数据结构。

这是我试图在 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;
      |                   ^~~~~~~~
 

【问题讨论】:

以后,请在您的问题中包含一个问题。您忽略了说明您是否只是要求解释为什么会发生此错误,或者您是否也对解决错误的方法感兴趣。 【参考方案1】:

第一条错误消息的要点是request(const request&amp;)(又名复制构造函数)被删除。 request 类不可复制。错误消息继续解释为什么 request 不可复制,但这是无关紧要的信息,除非您打算修改正在使用的库。

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

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

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

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

【讨论】:

以上是关于使用 boost 数据结构和 nghttp2 的 C++ 程序未编译的主要内容,如果未能解决你的问题,请参考以下文章

nghttp2:POST 有效载荷

如何使用 nghttp2 发送多块数据?

nghttp2 多部分 POST 消息

NgHttp2 调用请求数据处理程序两次,针对一个请求

cURL 不适用于 nghttp2

如何优雅地停止使用 nghttp2 asio 构建的 HTTP2 服务器