我在 Visual Studio 2015 中的 DEBUG 模式下编译时偶然发现了一个问题,其中出现访问冲突错误,代码如下(在 Release模式下没有问题,或者如果 _ITERATOR_DEBUG_LEVEL 1
或 2
)。自从我在项目属性中设置 _ITERATOR_DEBUG_LEVEL=0
后,问题才出现(为了简单起见,我在此处添加了 #define
,但这也会导致错误):
#define _ITERATOR_DEBUG_LEVEL 0
#include <iostream>
#include <string>
#include <sstream>
bool try_parse(const std::string& s, double& v)
{
std::stringstream stream;
std::locale invariant_number_locale{ std::locale{ "" }, new std::numpunct<char>() };
stream.imbue(invariant_number_locale);
stream << s;
stream >> v;
return !stream.fail();
}
int main()
{
double v;
std::string s = "123";
try_parse(s, v);
std::cout << "d = " << v << std::endl;
std::cout << "Done...";
std::getchar();
return EXIT_SUCCESS;
}
问题似乎出在 stream.imbue(invariant_number_locale);
行。
最佳答案
看来答案是,如果您使用构面,则必须按照此线程在 Debug模式下使用_ITERATOR_DEBUG_LEVEL=2
:
https://connect.microsoft.com/VisualStudio/feedback/details/2655363
感谢@HansPassant 为我指明了正确的方向。
关于c++ - Visual Studio 2015 中的 _ITERATOR_DEBUG_LEVEL 0 导致字符串流访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42715101/