我有这样的代码,当使用VS 2015 C++编译器进行编译时会产生警告。在VS 2015中的std :: wcstok
#include <cwchar>
#include <iostream>
int main()
{
wchar_t input[100] = L"A bird came down the walk";
wchar_t* token = std::wcstok(input, L" ");
while (token) {
std::wcout << token << '\n';
token = std::wcstok(nullptr, L" ");
}
}
这产生了以下警告。
warning C4996: 'wcstok': wcstok has been changed to conform with the ISO C standard, adding an extra context parameter. To use the legacy Microsoft wcstok, define _CRT_NON_CONFORMING_WCSTOK.
1> c:\program files (x86)\windows kits\10\include\10.0.10240.0\ucrt\corecrt_wstring.h(254): note: see declaration of 'wcstok'
warning C4996: 'wcstok': wcstok has been changed to conform with the ISO C standard, adding an extra context parameter. To use the legacy Microsoft wcstok, define _CRT_NON_CONFORMING_WCSTOK.
1> c:\program files (x86)\windows kits\10\include\10.0.10240.0\ucrt\corecrt_wstring.h(254): note: see declaration of 'wcstok'
看在线,我读到std::wcstok和breaking changes in VS 2015其中提到,C标准已经推出了第三个参数和
它使用的内部,每个线程上下文在调用跟踪状态, 和strtok一样。该函数现在具有签名
wchar_t* wcstok(wchar_t*, wchar_t const*, wchar_t**)
,并要求调用者 将上下文作为第三个参数传递给该函数。
在本质上听起来愚蠢,我还是会继续和问问成本, 可有人请解释简单来说,这第三个参数的目的,以及它是如何改变了从早期版本std::wcstok
?
在猜测,这是一样的strtod的最后一个参数。它包含下一个要解析的项目,因此可以在下一次调用时将其馈送到第一个参数中,这将使其线程安全。 – cup
全球CRT状态是一个非常讨厌的错误的食谱。 strtok()肯定是一个更邪恶的,errno,strerror(),asctime(),gmtime(),setlocale(),tmpfile(),tmpnam(),fcvt(),ecvt()值得荣誉提及。 –