简体   繁体   English

“localtime_s”:不是 VS2015 中“std”的成员

[英]'localtime_s': is not a member of 'std' in VS2015

The following C++ code does not compile with VS2015:以下 C++ 代码不能用 VS2015 编译:

#include <time.h>
#include <ctime>

void main()
{
    const time_t t = std::time(nullptr);

    std::tm tm = {};

    std::localtime_s(&tm, &time);
}

The error messages are:错误消息是:

Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86 Copyright (C) Microsoft Corporation. Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86 版权所有 (C) Microsoft Corporation。 All rights reserved.版权所有。

t.cpp t.cpp(10): error C2039: 'localtime_s': is not a member of 'std' C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\INCLUDE\\ctime(17): note: see declaration of 'std' t.cpp(10): error C2664: 'errno_t localtime_s(tm *const ,const time_t *const )': cannot convert argument 2 from 'time_t (__cdecl *)(time_t *const )' to 'const time_t *const ' t.cpp(10): note: There is no context in which this conversion is possible t.cpp t.cpp(10): error C2039: 'localtime_s': is not a member of 'std' C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\INCLUDE\\ctime(17): 注意:请参阅“std”t.cpp(10) 的声明:错误 C2664:“errno_t localtime_s(tm *const ,const time_t *const)”:无法将参数 2 从“time_t (__cdecl *)(time_t *const)”转换为“ const time_t *const ' t.cpp(10): 注意:没有可以进行这种转换的上下文

What can be a workaround?什么是解决方法? (using std::localtime() is not an option). (使用 std::localtime() 不是一种选择)。

Per [headers]/10[标题]/10

Annex K of the C standard describes a large number of functions, with associated types and macros, which “promote safer, more secure programming” than many of the traditional C library functions. C 标准的附录 K 描述了大量函数,以及相关的类型和宏,与许多传统的 C 库函数相比,它们“促进了更安全、更安全的编程”。 The names of the functions have a suffix of _s;函数名后缀为_s; most of them provide the same service as the C library function with the unsuffixed name, but generally take an additional argument whose value is the size of the result array.它们中的大多数提供与具有无后缀名称的 C 库函数相同的服务,但通常采用一个附加参数,其值为结果数组的大小。 If any C++ header is included, it is implementation-defined whether any of these names is declared in the global namespace.如果包含任何 C++ 头文件,则实现定义是否在全局命名空间中声明了这些名称中的任何一个。 (None of them is declared in namespace std.) (它们都没有在命名空间 std 中声明。)

Emphasis mine强调我的

and table 18 list localtime_s as a member so, because of the above section, we know that it is not defined in namespace std and if it does exist it will be in the global namespace.表 18 将localtime_s列为成员,因此,由于上面的部分,我们知道它没有在命名空间std定义,如果确实存在,它将在全局命名空间中。 Try using尝试使用

::localtime_s(&tm, &time);

and if that still does not work it means your implementation does not support it.如果这仍然不起作用,则意味着您的实现不支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM