1

Possible Duplicate:
Why is my log in the std namespace?

Based on Overload a C++ function according to the return value, I did the following experiment:

#include <cmath>

class myType
{
private:
    double value;
    myType(double value) : value(value) {}

public:

    myType& operator= (const myType& other) {
        if (this != &other) value = other.value;
        return *this;
    }

    static myType test(double val) { return myType(val); }

    friend std::ostream& operator<<(std::ostream& target, const myType& A);    
};

std::ostream& operator<<(std::ostream& target, const myType& A){
    target << A.value;
    return target;
}

class asin {
private:
    double value;
public:
    asin(double value)
        : value(std::asin( (value<-1.0) ? -1.0 : (value>1.0?1.0:value) ))
    {}
    operator double() { return value;               }
    operator myType() { return myType::test(value); }        
};


int main(int argc, char *argv[])
{
   myType d = asin(1.0); 

   std::cout << d << std::endl;

   return 0;
}

which resulted in

error: ‘myType::myType(double)’ is private

on the first line in main(). A bit more experimenting showed me that this works fine and as expected when I change the classname asin to Asin (or anything else for that matter). So apparently, I'm not allowed to call my class asin, while the act of defining it (and not using it) does not give me any warning/error.

Now I know all of this is bad practice, so don't flame me for that. I ask this purely out of academic interest: why can't I call my class asin, or acos or atan or anything like that? I was under the impression that cmath hid everything in the std-namespace, so that defining this class in the global namespace would not give rise this particular problem.

Can anyone explain what's going on?

Community
  • 1
  • 1
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96

2 Answers2

1

asin is a defined as global function in c++ standard library, and also defined in math library, (so you even dont have to use std::) If you try using unamed namespace then you would get 'use of `asin' is ambiguous' error. And named namespace solves your issue.

D Untouchable
  • 332
  • 4
  • 9
  • Thanks, although when I don't indluce `cmath` I don't have `asin` -- I'm guessing that's compiler dependent. I think the real question is: why doesn't the re-definition in `asin` in the global namespace cause an error? – Rody Oldenhuis Sep 24 '12 at 18:08
-1

For some IDEs, for example Visual Studio, you do not even need to include cmath. It has these functions already defined in the global namespace. Try to exclude cmath and you will see that asin() is still defined.

  • So is there a way on manually exclude those definitions? Would I just have to define my stuff in a different namespace? – AJMansfield Sep 24 '12 at 11:16
  • I'm coding mostly on Linux w/ gcc; I'm not very well-versed in Visual Studio. Doesn't VS include it, but then "hidden" inside things like `windows.h`? – Rody Oldenhuis Sep 24 '12 at 11:48
  • @AJMansfield I do not think you can simply exclude them. You should use namespaces. – Simon Meister Sep 24 '12 at 12:32
  • @Rody Oldenhuis: windows.h is not included. Unfortunately i can not find a list of already defined functions for msvc, but i think it is mostly math functions. – Simon Meister Sep 24 '12 at 12:35