小编jes*_*ses的帖子

<iostream> 和 #define __STDC_WANT_SECURE_LIB__ 0 导致错误 C2039:'sprintf_s'

在构建这个非常简单的测试程序时

#include <iostream>
int main() {
    std::cout << "x";
}
Run Code Online (Sandbox Code Playgroud)

使用 Visual Studio 2019,/Wall我得到了

warning C4668: '__STDC_WANT_SECURE_LIB__' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
Run Code Online (Sandbox Code Playgroud)

尝试去

#define __STDC_WANT_SECURE_LIB__ 0
Run Code Online (Sandbox Code Playgroud)

在包含 iostream 结果之前

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\xlocnum(1388,69): error C2039: 'sprintf_s': is not a member of '`global namespace''
Run Code Online (Sandbox Code Playgroud)

至少对于我的VS来说是这样。戈德博尔特没有抱怨

#define __STDC_WANT_SECURE_LIB__ 1
Run Code Online (Sandbox Code Playgroud)

很好,不会让编译器抱怨人们所期望的 sprintf_s 。

搜索时微软没有向我显示任何结果。这里也是如此,但总的来说,我找不到很多关于是否以及如何使用该定义的资源。

有没有办法禁用安全扩展并包含<iostream>?我是否使用了错误的定义或方法?

visual-c++ secure-scl visual-studio-2019

7
推荐指数
1
解决办法
1928
查看次数

如何使用 VS2019、/std:c++latest 和 /Zc:__cplusplus 编译 eigen

我正在尝试使用 VS2019 从 github 镜像编译 eigen 3.3.9,/std:c++latest并且/Zc:__cplusplus

我得到的是

eigen\Eigen\src\Core\util\Meta.h(320,25): error C2039: 'result_of': is not a member of 'std'
Run Code Online (Sandbox Code Playgroud)

因为EIGEN_HAS_STD_RESULT_OF已定义。这是在这里确定的:

#ifndef EIGEN_HAS_STD_RESULT_OF
#if EIGEN_MAX_CPP_VER>=11 && ((__has_feature(cxx_lambdas) || (defined(__cplusplus) && __cplusplus >= 201103L)))
#define EIGEN_HAS_STD_RESULT_OF 1
#else
#define EIGEN_HAS_STD_RESULT_OF 0
#endif
#endif
Run Code Online (Sandbox Code Playgroud)

据我所知, ,std::result_of在 C++20 中被删除,这没有反映在上面的检查中。

难道我做错了什么?有没有一种简单的方法可以使用 VS、C++20 和反映实际标准版本__cplusplus的定义来编译 eigen ,而无需自己手动设置所有定义或修补 Macros.h?

eigen c++20 visual-studio-2019

6
推荐指数
0
解决办法
844
查看次数

为什么这种检测习语的使用会导致 Clang 和 GCC 的编译错误不同,而 MSVC 不会导致编译错误

void_t尝试检测习惯用法时,我发现涉及减法运算符和 void* 的表达式会导致测试编译器中出现不同的错误。

GCC 10.2 及以下版本,无论使用 C++11、14、17 还是 20,似乎都将某些表达式视为硬错误而不是替换失败。

我正在使用以下(精简)示例

#include <type_traits>

template< class, class, class = void >
struct has_minus_void_t : std::false_type {};
template<class T, class U>
struct has_minus_void_t<T, U, std::void_t<
    decltype(T() - U())
    >> : std::true_type {};

static_assert(has_minus_void_t<int*,int*>::value, "int*");
static_assert(!has_minus_void_t<int*,void*>::value, "intvoid*");
static_assert(!has_minus_void_t<void*,int*>::value, "voidint*");
static_assert(!has_minus_void_t<void*,void*>::value, "void*");
Run Code Online (Sandbox Code Playgroud)

并且希望代码能够针对所有四种语言标准进行编译而不会出错(包含std::void_tC++17 之前的替代品,因此可以测试更多标准)但是

  • GCC 10.2 及以下版本抱怨最后两个断言
  • Clang 没有抱怨
  • MSVC 抱怨 int*/void* 组合

我试图搜索现有的错误报告,但找不到。

我想知道哪个编译器是正确的,为什么。

这是一个神箭

c++ standards-compliance decltype language-lawyer void-t

2
推荐指数
1
解决办法
62
查看次数