28

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

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.*。我检查了包含歧义声明的标题是什么,结果是:

数学调用.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>这里有什么问题,还是我误解了什么?

4

3 回答 3

25

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

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

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

于 2012-06-18T15:32:56.737 回答
3

如果你真的想要,你总是可以在 周围写一个小包装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;
}

我想额外的函数调用可能会有一些开销,这取决于编译器的聪明程度。

于 2012-10-09T17:52:00.770 回答
1

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

我已经处理这个问题很长时间了。一个案例是问题很明显的就是这个案例:

#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 库的 clang。)

最近我再次尝试解决这个问题。通过查看cmath(GCC),似乎头文件只是为了重载 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
}
于 2014-08-13T05:20:46.057 回答