This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
这是可能的。您可以采用与排序任何其他内容相同的方式来完成此操作:通过定义一个函数来比较两个具有严格总顺序的
std::any
。这行不通。
std::any::type
返回std::type_info
,除非您在中存储
时,std::type_info
类型的对象std::anystd::any_cast
将失败。有多种方法可以对异构类型的对象进行排序。
一个相对简单的方法是主要按对象的类型排序。这里需要注意的是,类型的顺序不可跨系统移植:
然后,可排序的相同类型的对象可能会进一步排序,但该功能可能必须仅限于一小组类型,就像您使用
std::变体
。It is possible. You can do it the same way as sorting anything else: By defining a function to compare two
std::any
with strict total order.This won't work.
std::any::type
returnsstd::type_info
, and unless you store an object of typestd::type_info
instd::any
, thestd::any_cast
will fail.There are many ways to order objects of heterogeneous types.
A relatively simple way is to primarily order by the type of the object. A caveat here is that the order of types is not portable across systems:
Then, objects of same type that are orderable may be further ordered, but that feature may have to be limited to a small set of types as if you were using
std::variant
.