I am developing a project which works with multiple arithmetic types. So I made a header, where the minimal requirements for a user defined arithmetic type are defined:
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); ... }
What is troubling me is that when I use code like this:
#include "user_defined_arithmetic.h" void some_function() { using namespace arithmetic; ArithmeticF lala(3); sin(lala); }
I get a compiler error:
error: call of overloaded 'sin(ArithmeticF&)' is ambiguous candidates are: double sin(double) const ArithmeticF arithmetic::sin(const ArithmeticF&)
I have never used the <math.h>
header, only the <cmath>
. I have never used the using namespace std
in a header file.
I am using gcc 4.6.*. I checked what is the header containing the ambiguous declaration and it turns out to be:
mathcalls.h :
Prototype declarations for math functions; helper file for <math.h>. ...
I know, that <cmath>
includes <math.h>
, but it should shield the declarations by the std namespace. I dig into the <cmath>
header and find:
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) { ...
So the namespace std begins after the #include <math.h>
. Is there something wrong here, or did I misunderstand something?
The cmath header file contains definitions for C++ for computing common mathematical functions. Include the standard header into a C++ program to effectively include the standard header < math. h > within the std namespace.
[cmath] defines symbols in the std namespace, and may also define symbols in the global namespace. [math. h] defines symbols in the global namespace, and may also define symbols in the std namespace. if you include the former and use an unqualified symbol, it may compile with one compiler but not with another.
<cmath> is a standard library header of C++ which is extended from C header <math. h> and comes with namespace std . Since C++ 17, special mathematical functions were merged into the standard from TR1 and linear interpolation function (C++ 20) which were included inside <cmath> header.
Cmath library is an essential part of the C++ programming language as it provides many operations and functions of mathematics when we use the programs to execute source codes. All of these functions are built-in functions.
Implementations of the C++ standard library are permitted to declare C library functions in the global namespace as well as in std
. Some would call this a mistake, since (as you've found) the namespace pollution can cause conflicts with your own names. However, that's the way it is, so we must live with it. You'll just have to qualify your name as arithmetic::sin
.
In the words of the standard (C++11 17.6.1.2/4):
In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace
std
. It is unspecified whether these names are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations (7.3.3).
If you really wanted to, you could always write a little wrapper around cmath
, along the lines of:
//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; }
I suppose there could be some overhead from the additional function call, depending on how clever the compiler is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With