I want to make a type to differentiate degrees and radians. I wanted something like this to produce a compiler error:

#include <iostream>

typedef double Degree;
typedef double Radian;

void MyFunction(Radian angle);

int main()
{
  Degree foo = 3.0;
  
  return 0;
}

void MyFunction(Radian angle)
{
  std::cout << "Success." << std::endl; 
}

But the typedef just translates directly to "double" so there is no problem passing a Degree to a function expecting a Radian.

I could make a struct and do

Radian foo;
foo.value = 3;
MyFunction(foo);
...
void MyFunction(Radian r);

But then I always have this ".value" hanging around everywhere. Is there no way to do what I'm looking to do?

Thanks,

Dave

How about two wrappers with implicit conversions?

class Radian {
  double value;
public:
  Radian ( double init ): value ( init ) {}
  operator double() { return value; }
};

class Degree {
  double value;
public:
  Degree ( double init ): value ( init ) {}
  operator double() { return value; }
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.