days and date types

H

Hicham Mouline

Hello,

we have been using an int type for both dates in the format YYYYMMDD and
number of days to some date.
We've had issues where both types got mixed at runtime.
We're looking for a simple C++ fix to this if there is, something like:

struct Date {
int i;
};

struct Days {
int i;
};


void f( Date d );

....
const Days d = { 15 };
f( d ); // compile-error


This would help detect at compile-time errors like these.
Is there a runtime cost of using struct instead of int, in these cases?

regards,
 
M

Michael Tsang

Hicham said:
struct Date {
int i;
};

struct Days {
int i;
};


void f( Date d );

...
const Days d = { 15 };
f( d ); // compile-error

It's because you tried to pass a "const Days &"
to a "Date", and a "const Days &" cannot be converted to a "Date".
 
J

Jorgen Grahn

Hello,

we have been using an int type for both dates in the format YYYYMMDD and
number of days to some date.
We've had issues where both types got mixed at runtime.
We're looking for a simple C++ fix to this if there is, something like:

struct Date {
int i;
};

struct Days {
int i;
};


void f( Date d );

...
const Days d = { 15 };
f( d ); // compile-error


This would help detect at compile-time errors like these.
Is there a runtime cost of using struct instead of int, in these cases?

As far as I know, none worth mentioning, if you do it reasonably well.
It's important for a C++ compiler to make this cheap: the standard
library iterators etc need it to compete with raw pointers.

But -- your solution is a C solution. That's painful. Why not

class Days {
public:
Days() : d(0) {}
explicit Days(int days) : d(days) {}

// operators etc which make sense, e.g.
// output, addition and ++, but not multiplication
private:
int d;
};

const Days d(15);

Otherwise, I definitely agree with you. Typedef:ing ints is just
asking for the kind of problem you describe. Also see this discussion,
"Simple and clear ways of creating distinct types":
Message-ID: <[email protected]>

/Jorgen
 
A

AnonMail2005

Hello,

we have been using an int type for both dates in the format YYYYMMDD and
number of days to some date.
We've had issues where both types got mixed at runtime.
We're looking for a simple C++ fix to this if there is, something like:

struct Date {
  int i;

};

struct Days {
  int i;

};

void f( Date d );

...
const Days d = { 15 };
f( d );  // compile-error

This would help detect at compile-time errors like these.
Is there a runtime cost of using struct instead of int, in these cases?

regards,

Date should be a class. Valid values of Date are a subset of int and
you can use the constructor to enforce validity. But it sounds like
days really is just an int as far as I can tell from your post.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top