【问题标题】:Why does this std::map not display in a useable way in the watch window of Visual C++?为什么这个 std::map 不能在 Visual C++ 的监视窗口中以可用的方式显示?
【发布时间】:2020-09-17 05:34:43
【问题描述】:

我无法在监视窗口中查看某些 std::map。查看 .natvis 文件,std::map 有多种实现。有没有办法选择其中一个?

https://developercommunity.visualstudio.com/content/problem/1056550/im-unable-to-inspect-a-variable-of-type-stdmap-in.html

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <set>
#include <memory>

typedef std::shared_ptr<std::string> PTR_STRING;
typedef std::map<PTR_STRING, std::size_t> accessFunction2Order;
typedef std::set<accessFunction2Order> setOfAccessFunction2Order;
typedef std::map<std::vector<std::size_t>, setOfAccessFunction2Order> A2B;
typedef std::map<PTR_STRING, std::shared_ptr<A2B> > MAP;

int main()
{   MAP s{
        {    std::make_shared<std::string>("asdasdasdasdasdasdasdasdasdasd"),
            std::make_shared<A2B>()
        }
    };
    const auto &r1 = *s.begin();
}

地图 s 不能被观看(关于 std::_Tree 正在显示)。奇怪的是可以引用第一个元素。

【问题讨论】:

  • 我认为这可能与最大递归深度有关。我们还在处理 8 位 CPU 的地址空间限制吗?

标签: visual-studio visual-c++ visual-studio-2017 natvis


【解决方案1】:

问题是由 Visual Studio 调试器中的(我认为是硬编码的)限制引起的。 为了显示一个变量,调试器正在调整他在 .natvis 文件中找到的内容——但在尝试了一些固定次数的解析类型后他放弃了。

解决这个问题的方法是使用 std::any 之类的东西

(或 boost::any 对于我们这些无法使用最新 C++ 版本的人来说)

将此 STL 类型分解为调试器可以处理的块。 这当然只是一种解决方法。 希望这个问题早日解决。

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <set>
#include <any>
#include <memory>

typedef std::shared_ptr<std::string> PTR_STRING;
typedef std::map<PTR_STRING, long> accessFunction2Order;
typedef std::set<accessFunction2Order> setOfAccessFunction2Order;
#if 1
typedef std::map<std::vector<std::size_t>, std::any> A2B;
#else
typedef std::map<std::vector<std::size_t>, setOfAccessFunction2Order> A2B;
#endif
typedef std::map<PTR_STRING, std::shared_ptr<A2B> > MAP;
typedef std::shared_ptr<std::size_t> PTR_INT;

int main()
{   const MAP s{
        {        std::make_shared<std::string>("asdasdasdasdasdasdasdasdasdasd"), 
             std::make_shared<A2B>()
        }
    };
}

【讨论】:

    猜你喜欢
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多