Why are some functions in not in the std namespace?

前端 未结 3 821
猫巷女王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 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
    #include
    
    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
    }
    //instead of #include
    

    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 but most functions seem to work.

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

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

    namespace std{
       #include // compile errors
    }
    

提交回复
热议问题