- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试包装 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 .
根据 here , boost::variant
应该接受 T &&
并用它来实例化 T
内部。
根据 here , boost::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
的默认移动构造函数。有一个 Unresolved 问题使 ssl::stream
在引用 chriskohlhoff/asio#124 的 boost bugtracker 中移动可构造。
该问题的解决方案是使用 ssl::stream
的可移动构造实现,就像 Beast 示例 here 中提供的那样,它看起来可能在 处可用boost/beast/experimental/core/ssl_stream.hpp
在更高版本的 Beast 中(不在 1.67.0 中)。
关于c++ - 为什么 Boost Variant 使用模板构造函数而不是 boost::beast::websocket::stream 的移动构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51585147/
我创建了一个简单的 Variant 类来存储字符串、整数、 double 等。我正在尝试使用 std::map 类型的映射但我收到了这个奇怪的错误: In file included from /us
我有一个整数数组来检索 std::variant 中的内容。但是编译失败,报错No matching function to call 'get' .您能否解释原因,并提出实现相同目的的可行解决方案?
我的问题涉及 boost::variant 转换与 C++ 中的 std::vector 的混合。在我的项目中,我使用变体作为 SQL 的输出。我将始终只使用一种类型的变体。我想要做的是从变量、变量
(警告:虽然它乍一看可能是一个问题,但这是 而不是 一个初学者级别的问题。如果您熟悉“让强制”这个短语或者您曾经查看过 VBA 规范,请继续阅读。) 假设我有一个 Variant 类型的表达式,我想将
我正在使用一个相当笨拙的 c 接口(interface)来存储集合。 LowLevelStorer 类表示我为此接口(interface)编写的包装器。 Storer 类是一个高级类,它与Data 有
我试图将两个变体组合成一个变体只是为了便于阅读。这是代码: using VariantType_basic = std::variant; using VariantType_vector = std
给定一个类型为 std::variant 的变量,我检查过它不包含C .如何将其转换为 std::variant ? std::variant convert(std::variant value)
使用boost:variant: #include #include #include template boost::variant _tuple_index(size_t i, const
在 answer 中对于这个 SO 问题: What is the equivalent of boost::variant in the C++ standard library? 提到boost:
自从在 Android Gradle 插件 0.13.0 中升级到 gradle 2.1 后,这个问题就出现了,但我一直无法理解为什么有时会记录此警告。 考虑此 block 以根据变体类型重命名 AP
我想为变量实现一个模板方法。 但是根据输入是否为 int 变量(char、short、int),我想处理它与输入为 float 变量(float、double、long double)的情况有所不同。
boost::variant通过 boost::variant<>::types 公开其变体类型列表, 可以方便地与 boost::mpl::for_each 一起使用. std::variant缺少
歌词: 我尝试通过 MPI 实现任务池。所以我需要某种 RPC,但它可以在我的程序的不同部分之间工作,这意味着处理器 A 希望处理器 B 以参数 D 调用函数 C。我们不能像处理线程那样在进程之间传递
目前我的库使用 boost::optional 和 boost::variant。由于 C++17 已经发布,我想添加一个选项,它可以与 boost 和 std 一起使用。 所以我成功地测试了带有 b
假设我有: class TypeA { }; class TypeB { }; typedef boost::variant Type; 没关系: void foo(Type t) { }; int
假设我有一个嵌套的 boost::variant -类型TNested包含一些类型和一些其他 boost::variant类型(它本身不能再次包含 boost::variant types ,因此不会
使用带有 gradle 插件版本 3.3.0-alpha11 的 Android Studio 3.3 Canary 11。尝试同步 gradle 时会抛出以下错误 WARNING: API 'var
我看过这篇关于使用 std::variant 的文章.这是因为以下代码引发了代码分析警告: void CChristianLifeMinistryHtmlView::OnTimer(UINT_PTR
我有一个 Delphi 6 类对象,其中包含 30 个变体的数组,每个变体都通过不同的索引属性公开。例如: property responseCode: integer Index 7
...或在内部快速更改类型 std::variant在源代码中。 下面是列表本身及其容器元素的头文件中的代码。 // HVector.hh class HVector: public std::vec
我是一名优秀的程序员,十分优秀!