为什么某些函数<cmath>不在 std 命名空间中?



我正在开发一个使用多种算术类型的项目。所以我做了一个标题,其中定义了用户定义的算术类型的最低要求:

user_defined_arithmetic.h :

typedef double ArithmeticF;   // The user chooses what type he 
                              // wants to use to represent a real number
namespace arithmetic          // and defines the functions related to that type
{
const ArithmeticF sin(const ArithmeticF& x);
const ArithmeticF cos(const ArithmeticF& x);
const ArithmeticF tan(const ArithmeticF& x);
...
}

令我困扰的是,当我使用这样的代码时:

#include "user_defined_arithmetic.h"
void some_function()
{
    using namespace arithmetic;
    ArithmeticF lala(3);
    sin(lala);
}

我收到编译器错误:

error: call of overloaded 'sin(ArithmeticF&)' is ambiguous
candidates are:
double sin(double)
const ArithmeticF arithmetic::sin(const ArithmeticF&)

我从未使用过<math.h>标题,只使用<cmath>.我从未在头文件中使用过using namespace std

我正在使用 gcc 4.6.*。我检查了包含模棱两可声明的标题是什么,结果是:

mathcalls.h :

Prototype declarations for math functions; helper file for <math.h>.
...

我知道,<cmath>包括<math.h>,但它应该通过 std 命名空间屏蔽声明。我深入研究<cmath>标题并发现:

cmath.h :

...
#include <math.h>
...
// Get rid of those macros defined in <math.h> in lieu of real functions.
#undef abs
#undef div
#undef acos
...
namespace std _GLIBCXX_VISIBILITY(default)
{
...

所以命名空间 std #include <math.h> 之后开始。是这里出了什么问题,还是我误解了什么?

C++ 标准库的实现允许在全局命名空间和 std 中声明 C 库函数。有些人会称这是一个错误,因为(正如你发现的)命名空间污染会导致与你自己的名字冲突。然而,事情就是这样,所以我们必须忍受它。你只需要把你的名字限定为arithmetic::sin

用标准(C++11 17.6.1.2/4)的话来说:

但是,在C++标准库中,声明(除了 在 C) 中定义为宏的名称在命名空间std的命名空间范围 (3.3.6) 内。是的 未指定这些名称是否首先在全局命名空间范围内声明,然后注入 通过显式使用声明 (7.3.3) 进入命名空间 std。

如果你真的想,你可以随时在cmath周围写一个小包装器,大致如下:

//stdmath.cpp
#include <cmath>
namespace stdmath
{
    double sin(double x)
    {
        return std::sin(x);
    }
}
//stdmath.hpp
#ifndef STDMATH_HPP
#define STDMATH_HPP
namespace stdmath {
    double sin(double);
}
#endif
//uses_stdmath.cpp
#include <iostream>
#include "stdmath.hpp"
double sin(double x)
{
    return 1.0;
}
int main()
{
    std::cout << stdmath::sin(1) << std::endl;
    std::cout << sin(1) << std::endl;
}

我想附加函数调用可能会有一些开销,具体取决于编译器的聪明程度。

这只是开始解决这个问题的谦虚尝试。(欢迎提出建议。

我已经处理这个问题很长时间了。一个案例的问题非常明显是这种情况:

#include<cmath>
#include<iostream>
namespace mylib{
    std::string exp(double x){return "mylib::exp";}
}
int main(){
    std::cout << std::exp(1.) << std::endl; // works
    std::cout << mylib::exp(1.) << std::endl; // works
    using namespace mylib;
    std::cout << exp(1.) << std::endl; //doesn't works!, "ambiguous" call
    return 0;
}

在我看来,这是一个烦人的错误,或者至少是一个非常不幸的情况。(至少在 GCC 中,在 Linux 中使用 GCC 库是这样。

最近我又试了一次这个问题。通过查看(GCC)的cmath,似乎标头的存在只是为了重载C函数并在此过程中搞砸命名空间。

namespace std{
   #include<math.h>
}
//instead of #include<cmath>

有了它,这就可以了

using namespace mylib;
std::cout << exp(1.) << std::endl; //now works.

我几乎可以肯定这并不完全等同于#include<cmath>但大多数功能似乎都可以工作。

最糟糕的是,最终某些依赖库最终会#inclulde<cmath>。为此,我还找不到解决方案。

注意:不用说这根本不起作用

namespace std{
   #include<cmath> // compile errors
}

最新更新