[英]cmath error on MacOS c++17: call to abs ambiguous
Having this error using c++17 on Mac OS.在 Mac OS 上使用 c++17 出现此错误。 As far as I can tell, code is correct and should work fine (compiles without issue w/ g++ and clang++ on linux).据我所知,代码是正确的,应该可以正常工作(在 linux 上使用 g++ 和 clang++ 编译没有问题)。 Also, as far as I can tell, the current default mac version of clang [10.0.1] should support c++17 (full version info printout below).另外,据我所知,clang [10.0.1] 的当前默认 mac 版本应该支持 c++17(完整版本信息打印输出如下)。
So, my question is: is this actually a bug in my code, but it works by fluke on linux?所以,我的问题是:这实际上是我代码中的一个错误,但它在 linux 上被侥幸成功了吗? Or is it an issue with MacOS clang eg, not full c++17 implementation?或者是 MacOS clang 的问题,例如不是完整的 c++17 实现?
From cppref: Defined in header (since C++17): int abs( int n );
来自 cppref:在头文件中定义(C++17 起): int abs( int n );
Other c++17 features seem to work completely fine.其他 c++17 功能似乎完全正常。
#include <cmath>
// #include <cstdlib> //works if included
int main() {
int i = 1;
// return std::abs(1); // Works fine
return std::abs(i); // Fails
}
Compile with: clang++ -std=c++17 test.cpp
编译: clang++ -std=c++17 test.cpp
Get this error:得到这个错误:
test.cpp:7:10: error: call to 'abs' is ambiguous
return std::abs(i);
^~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/math.h:761:1: note:
candidate function
abs(float __lcpp_x) _NOEXCEPT {return ::fabsf(__lcpp_x);}
^
(... etc.)
1 error generated.
If you #include <cstdlib>
, it works without error.如果你#include <cstdlib>
,它可以正常工作。 using -std=gnu++17
or -std=c++1z
doesn't remove the problem either.使用-std=gnu++17
或-std=c++1z
也不能解决问题。 In the actual code (which is obviously more complex than the above, and actually uses c++17 features), the error happens depending on the order of my include files.在实际代码中(显然比上面的更复杂,并且实际上使用了 c++17 特性),错误的发生取决于我的包含文件的顺序。 I can't replicate that in the simple example, but I assume it boils down to calling the cstdlib
version instead of the cmath
version.我无法在简单的示例中复制它,但我认为它归结为调用cstdlib
版本而不是cmath
版本。
Currently, my 'workaround' is to just put the header includes into the order that works..but this is hardly a long-term solution.目前,我的“解决方法”只是将标题包含到有效的顺序中......但这几乎不是一个长期的解决方案。
Does anyone know the cause?有谁知道原因?
Version info (error not specific to this MacOS version, also happens on my students' laptops):版本信息(错误并非特定于此 MacOS 版本,也发生在我学生的笔记本电脑上):
Bens-iMac:test ben$ clang++ -v
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
See LWG Issue 2912请参阅LWG 问题 2912
This has been fixed in libc++ trunk.这已在 libc++ 主干中修复。 I don't know if Apple has shipped this fix yet.我不知道 Apple 是否已经发布了此修复程序。 As you found, including <cstdlib>
is a workaround.如您<cstdlib>
,包括<cstdlib>
是一种解决方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.