【发布时间】:2021-12-24 23:23:32
【问题描述】:
我在link 上玩了一点在线 c++ 编译器。但是下面的代码 sn-p 在用 msvc v19.latest 编译时失败了。
#include <iostream>
#include <cmath>
#include <cstdio>
template<class F, class...L>
void test(F f, L...args) {
std::cout<< "res = " << f(args...) << '\n';
}
int main()
{
test(cos, 0.1); #1
test(printf, "%s", "aaa"); #2
}
怎么可能2号线没问题,1号线通不过呢?
MSVC 对以下代码很满意,但这次轮到 GCC 拒绝它了。 MSVC 的 iostream 文件包括 cmath 头和 GCC #undefs cos :)
#include <stdio.h>
#include <math.h>
//#include <iostream>
template<class F, class...L>
void test(F f, L...args) {
f(args...);
}
int main()
{
test(cos, 0.1);
test(printf, "%s", "aaa");
}
从 c++20 开始,第二个答案中提出了另一个问题,并已在此问题中解决link
【问题讨论】:
-
您的神螺栓链接指向默认登录页面。您需要选择 share 作为永久链接。
-
printf函数只有一个。cos被重载了,你需要指定你想要的重载。 -
另请注意,仅使用
cos是不可移植的。cmath不需要在全局命名空间中公开任何函数。事实上,如果您使用std::cos使其正确,gcc 也将无法编译代码:godbolt.org/z/q1q5qcjva -
test(static_cast<double(*)(double)>(&std::cos), 0.1); -
@Eljay:
cos是否在我们可以获取地址的允许函数中?
标签: c++ visual-c++