我有一个http服务器,是通过使用ghttp2创建的。在这个服务器上,我试图使用boost的circular_buffer
数据结构。
这是我的c++程序,我试图在Ubuntu 20.04上编译。
#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&)
(又名复制构造函数)被删除了。request
类不可复制。错误信息继续解释为什么request
不可复制,但除非您打算修改您正在使用的库,否则这是无关紧要的信息。
由于request
是(隐式)不可复制的,所以lambda不允许将其req
参数的副本压入容器(例如,当您尝试时,压入boost::circular_buffer
)。你必须想出另一个办法。
req
参数是const
引用,因此不允许从req
移动。
(有些人会错误地尝试的一种方法是让lambda在容器中存储指向req
形参的指针。不要这样做,因为不能保证指针在当前对lambda的调用完成后仍然有效。
看起来目的是让req
完全被处理程序处理。一个语法上有效的选项是从req
中提取必要的数据,并将该数据的副本存储在您自己定义的结构中。这是否明智已经超出了语法问题的范围;它进入了ghttp2库的预期用途(我不熟悉的东西)。