关键词:
【中文标题】具有 std::map 和 std::variant 的不完整类型【英文标题】:incomplete types with std::map and std::variant 【发布时间】:2018-11-08 09:31:26 【问题描述】:考虑在std::variant
之上递归变体的这种简化且非常具体的实现:
#include <map>
#include <variant>
struct recursive_tag;
template <typename...>
struct RecursiveVariant;
template <>
struct RecursiveVariant<int, std::map<int, recursive_tag>>
: std::variant<int, std::map<int, RecursiveVariant<int, std::map<int, recursive_tag>>>>
using underlying = std::variant<int,
std::map<int, RecursiveVariant<int, std::map<int, recursive_tag>>>>;
using underlying::underlying;
;
int main()
RecursiveVariant<int, std::map<int, recursive_tag>> rv;
由于尝试实例化 std::pair<const int, recursive_tag>
而无法在 gcc 7/8 上编译,而这本身会失败,因为 recursive_tag
是不完整的类型。
但是,编译器错误调用堆栈中的任何内容都没有告诉我为什么 std::pair<const int, recursive_tag>
需要被实例化。最上面一行是:
variant:252:48: 需要来自‘
void std::__detail::__variant::__erased_dtor(_Variant&&) [with _Variant = const std::__detail::__variant::_Variant_storage<false, int, std::map<int, RecursiveVariant<int, std::map<int, recursive_tag, std::less<int>, std::allocator<std::pair<const int, recursive_tag> > > >, std::less<int>, std::allocator<std::pair<const int, RecursiveVariant<int, std::map<int, recursive_tag, std::less<int>, std::allocator<std::pair<const int, recursive_tag> > > > > > > >&; long unsigned int _Np = 0]
’
指向:
249 template<typename _Variant, size_t _Np>
250 void
251 __erased_dtor(_Variant&& __v)
252 std::_Destroy(std::__addressof(__get<_Np>(__v)));
虽然在其中拼写了 map<int, recursive_tag>
类型,但应该实例化的实际 map
类型是 map<int, RecursiveVariant<int, map<int, recursive_tag>>>
... 这应该只需要实例化 pair<const int, RecursiveVariant<...>>
。
只需使recursive_tag
完整(即通过添加)即可解决问题。但问题的根源是什么?
【问题讨论】:
【参考方案1】:line at issue 来电
std::_Destroy(std::__addressof(__get<_Np>(__v)));
对__get
执行ADL 的需要足以触发__v
类型的任何和所有关联类的实例化,即_Variant
,以寻找潜在的朋友函数(和函数模板)在这些类中定义的名称。这包括让你绊倒的pair
。
【讨论】:
Argh,该文件中如何还有更多对该函数的不合格调用。我敢肯定新的会在一夜之间出现。从具有自定义类型的 c++ 中的 std::map 获取值
】从具有自定义类型的c++中的std::map获取值【英文标题】:getvaluefromstd::mapinc++whichhascustomtype【发布时间】:2015-03-3115:53:39【问题描述】:我已将地图初始化为:typedefvoid*ProxyClientHandler;std::map<string,ProxyClientHandler>connectedClient;... 查看详情
带有键的 std::map 作为具有三个 int 成员的结构 [重复]
】带有键的std::map作为具有三个int成员的结构[重复]【英文标题】:std::mapwithkeyasastructwiththreeintmembers[duplicate]【发布时间】:2021-06-2906:15:31【问题描述】:我想使用具有三个整数成员的结构作为键。如何重载booloperator<(constCacheKe... 查看详情
如何找到 std::unordered_map 的当前容量?
...2020-06-1007:10:47【问题描述】:std::unordered_map和std::vector都具有reserve方法,这增加了集合的容量,因此可以在不扩大内部缓冲区的情况下向集合中添加多个元素。但是,std::vector也有一个方法cap 查看详情
C++ 将预先保留的哈希映射(std::unordered_map)与整数键和连续数据数组(std::vector)进行比较
...or)【发布时间】:2021-01-1116:35:03【问题描述】:假设使用具有int键类型的哈希映射结 查看详情
在 std::map 和 std::unordered_map 之间进行选择 [重复]
】在std::map和std::unordered_map之间进行选择[重复]【英文标题】:Choosingbetweenstd::mapandstd::unordered_map[duplicate]【发布时间】:2011-04-2314:11:32【问题描述】:既然std在unordered_map中有一个真正的哈希映射,为什么(或何时)我仍想在实际... 查看详情
C++ std::map 和 std::vector 的优点? [关闭]
】C++std::map和std::vector的优点?[关闭]【英文标题】:AdvantagesofC++std::mapandstd::vector?[closed]【发布时间】:2013-01-0920:42:42【问题描述】:周末我将参加一个编程比赛,我想知道我应该使用std::vector还是std::map?我会简单地将它们用作... 查看详情
Cython C++ 和 std::map 处理
】CythonC++和std::map处理【英文标题】:CythonC++andstd::maphandling【发布时间】:2019-10-0714:11:04【问题描述】:我正在尝试将我的C++类与Cython接口,但难以作为参数传递std::map。sample.pxd文件:fromlibcppcimportboolfromlibcppcimportstringfromlibcppcim... 查看详情
c++模板和迭代器
...::map<T,double>::iterator>vec)//....我希望函数在此映射上具有一个映射 查看详情
std::map 糟糕的字符仅在发布模式下
...std::map作为函数的参数,并且在调试模式(VS2008)中,映射具有正确的键和值,但是当我在Release中启动程序时,映射中有糟糕的字符!代码转储:std::map<CString,CString>libVersions 查看详情
std::map 和性能,相交集
】std::map和性能,相交集【英文标题】:std::mapandperformance,intersectingsets【发布时间】:2009-06-2901:39:13【问题描述】:我正在交叉一些数字集,并通过存储每次在地图中看到数字时的计数来做到这一点。我发现性能非常缓慢。详情... 查看详情
c++std::map和std::unordered_map区别时间复杂度适用
std::map对应的数据结构是红黑树。红黑树是一种近似于平衡的二叉查找树,里面的数据是有序的。在红黑树上做查找、插入、删除操作的时间复杂度为O(logN)。而std::unordered_map对应哈希表,哈希表的特点就是查找效率高... 查看详情
std::map的insert和下标[]操作区别
...也有区别。insert接受一个pair参数,并且返回一个pair,以std::map<int,int>为例,其返回值是一个std::pair<std::map<int,int>::iterator,bool>,如果数据插入成功(key不存在)则返回的迭代器second为true且first返回插入元素的迭代器... 查看详情
std::map 和线程安全的奇怪问题
】std::map和线程安全的奇怪问题【英文标题】:Oddissuewithstd::mapandthreadsafety【发布时间】:2011-03-0710:54:41【问题描述】:现在这不是什么大问题,因为我已经实现了自己的集合,但对这个仍然有点好奇。我有一个单例,它提供对... 查看详情
如何从具有最高值的 unordered_map 中获取密钥?
】如何从具有最高值的unordered_map中获取密钥?【英文标题】:Howtogetthekeyfromanunordered_mapwiththehighestvalue?【发布时间】:2021-04-2500:00:02【问题描述】:我有以下代码查看是否存在键,如果存在,则返回键和值:std::unordered_map<std::... 查看详情
使用 SWIG 和 Python/C API 包装返回 std::map 的函数
】使用SWIG和Python/CAPI包装返回std::map的函数【英文标题】:UsingSWIGandthePython/CAPItowrapafunctionwhichreturnsastd::map【发布时间】:2014-10-1021:05:11【问题描述】:我想包装一个C++例程,它返回一个std::map整数和指向C++类实例的指针。我无法... 查看详情
std::multimap<key, value> 和 std::map<key, std::set<value> 有啥区别?
】std::multimap<key,value>和std::map<key,std::set<value>有啥区别?【英文标题】:What\'sthedifferencebetweenstd::multimap<key,value>andstd::map<key,std::set<value>>std::multimap<key,value>和std::ma 查看详情
线程安全std::map:锁定整个地图和各个值[重复](代码片段)
这个问题在这里已有答案: ThreadsafetyofC++stdContainers4个答案 structData...CRITICAL_SECTIONvalLock;std::map<int,Data>mp;CRITICAL_SECTIONmpLock;我目前正在使用两个关键部分来使这个线程安全。我必须锁定map和Data 查看详情
boost::unordered_map和std::map的对比(包括速度和内存消耗)(代码片段)
...适的数据结构对其进行存储。本文对比boost::unordered_map和std::map这两种数据结构在该使用情景下的效率。代码:#include"boost/unordered_map.hpp"#include<iostrea 查看详情