`cosf`,`sinf`等不在`std`中 [英] `cosf`, `sinf`, etc. are not in `std`

查看:270
本文介绍了`cosf`,`sinf`等不在`std`中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于此处的讨论,我有向Ubuntu开发人员报告了一个错误.

Based on the discussion here, I have reported a bug to the Ubuntu developers.

在编译以下示例c ++程序时:

When compiling the following sample c++ program:

#include <cmath>
#include <stdio.h>

int main()
{
    printf("%f\n", std::cosf(0.0f));
}

我收到以下错误消息:error: ‘cosf’ is not a member of ‘std’

I get the following error message: error: ‘cosf’ is not a member of ‘std’

包含math.h并使用非命名空间版本可以正常工作.发生了什么事?

Including math.h and using the non-namespaced version works fine. What is going on?

我正在Ubuntu 19.04上使用g ++ 8.3.0-6ubuntu1.

I am using g++ 8.3.0-6ubuntu1 on Ubuntu 19.04.

我正在使用g++ --std=c++17 test.cpp

推荐答案

该版本的库(有未解决的错误报告.

That version of the library (libstdc++8) is not fully conforming to C++17. The copyright notice says it was last updated in 2016. As of June 2019, the latest upstream release is bugged. It does have a #if __cplusplus > 201402L section, but it doesn’t declare the identifiers required by C++17. There is an open bug report.

查看Ubuntu上的/usr/include/c++/8/cmath,它包括<math.h>,未定义其功能的一系列宏(C标准库要求)以访问其名称,将cosacos等导入到其中. std::名称空间,然后将重载的floatlong double重载声明为inline.

Looking at /usr/include/c++/8/cmath on Ubuntu, it includes <math.h>, undefines a series of macros for its functions (required by the C standard library) to access their names, imports cos, acos, etc. into the std:: namespace, and then declares the overloaded float and long double overloads as inline.

即使 C ++ 11标准说,在C中定义为函数的名称应在C ++标准库中定义为函数",并且使用外部链接声明的标准C库中的每个名称都保留给实现,以用作带有extern "C"链接的名称,无论是在namespace std中还是在全局命名空间中."但是,它没有明确指出std::expf等.必须得到支持,直到2016年6月 P0175r1.这显然是疏忽.

It never declares cosf within the std:: namespace, even though C++17 says it shall. The C++11 standard says, "Names that are defined as functions in C shall be defined as functions in the C++ standard library," and "Each name from the Standard C library declared with external linkage is reserved to the implementation for use as a name with extern "C" linkage, both in namespace std and in the global namespace." However, it does not explicitly state that std::expf et al. must be supported until P0175r1 in June 2016. This was apparently an oversight.

libc ++ 库确实声明了它们,因此使用clang++ -std=c++17 -stdlib=libc++进行编译应该可以.

The libc++ library does declare them, so compiling with clang++ -std=c++17 -stdlib=libc++ should work.

这篇关于`cosf`,`sinf`等不在`std`中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆