Why are some functions in not in the std namespace?

前端 未结 3 803
猫巷女王i
猫巷女王i 2020-11-30 06:50

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:

相关标签:
3条回答
  • 2020-11-30 06:59

    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).

    0 讨论(0)
  • 2020-11-30 07:00

    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.

    0 讨论(0)
  • 2020-11-30 07:17

    This is just a humble attempt to start solving this problem. (Suggestions are welcomed.)

    I have been dealing with this problem a long time. A case were the problem is very obvious is this case:

    #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;
    }
    

    This is in my opinion is an annoying bug or at the least an very unfortunate situation. (At least in GCC, and clang --using GCC library-- in Linux.)

    Lately I gave it another shot to the problem. By looking at the cmath (of GCC) it seems that the header is there simply to overload the C-functions and screws up the namespace in the process.

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

    With it this works

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

    I am almost sure that this is not exactly equivalent to #include<cmath> but most functions seem to work.

    Worst of all is that eventually some dependence library will eventually will #inclulde<cmath>. For that I couldn't find a solution yet.

    NOTE: Needless to say this doesn't work at all

    namespace std{
       #include<cmath> // compile errors
    }
    
    0 讨论(0)
提交回复
热议问题