473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Unique Integer Types -- a useful C++ trick

This has probably been done a zillion times before, but it is new to
me (and some of the folks I work with), and also quite useful.

Let me know if there are obvious improvements that I have missed. :-)

http://www.colohan.com/docs/unique_integers.html

Chris
--
Chris Colohan Email: ch***@colohan.ca PGP: finger co*****@cs.cmu.edu
Web: www.colohan.com Phone: (412)268-4751
Jul 22 '05 #1
4 1159
Christopher Brian Colohan wrote:
This has probably been done a zillion times before, but it is new to
me (and some of the folks I work with), and also quite useful.

Let me know if there are obvious improvements that I have missed. :-)

http://www.colohan.com/docs/unique_integers.html


Preventing conversion-construction from anything is usually done using
the 'explicit' keyword. I think you might find looking into it useful.
It would be nice not to have the number stated as the template argument
when declaring typedefs. I am not sure it's possible, but I suspect
that it is.

In any case, interesting work.

V
Jul 22 '05 #2

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:O9****************@newsread1.dllstx09.us.to.v erio.net...
Christopher Brian Colohan wrote:
This has probably been done a zillion times before, but it is new to
me (and some of the folks I work with), and also quite useful.

Let me know if there are obvious improvements that I have missed. :-)

http://www.colohan.com/docs/unique_integers.html
It would be nice not to have the number stated as the template argument
when declaring typedefs. I am not sure it's possible, but I suspect
that it is.


It can almost be done with CRTP:

struct set_num_t : unique_int<set_num_t> { };

The trouble is you lose the constructor taking an unsigned int and the private
constructor taking a bool. If you don't mind macros, you could do

#define UNIQUE_INT(name) \
struct name : unique_int<name> { \
private: name(bool); \
public: name(unsigned v = 0u) : unique_int<name>(v) { }\
}; \
/**/

UNIQUE_INT(set_num_t)
UNIQUE_INT(entry_num_t)
UNIQUE_INT(lru_entry_t)

Jonathan


Jul 22 '05 #3
>
The trouble is you lose the constructor taking an unsigned int and the private
constructor taking a bool. If you don't mind macros, you could do

#define UNIQUE_INT(name) \
struct name : unique_int<name> { \
private: name(bool); \
public: name(unsigned v = 0u) : unique_int<name>(v) { }\
}; \
/**/

UNIQUE_INT(set_num_t)
UNIQUE_INT(entry_num_t)
UNIQUE_INT(lru_entry_t)


Macros are cool. It would have been great if Bjarne had improved the
preprocessor as well when he invented the C++ language... perhaps it
is still possible with the new C++ standard coming up?
--
http://www.it-is-truth.org/
Jul 22 '05 #4

"Asfand Yar Qazi" <se*@message.body.com> wrote in message
news:30*************@uni-berlin.de...

The trouble is you lose the constructor taking an unsigned int and the private constructor taking a bool. If you don't mind macros, you could do

#define UNIQUE_INT(name) \
struct name : unique_int<name> { \
private: name(bool); \
public: name(unsigned v = 0u) : unique_int<name>(v) { }\
}; \
/**/

UNIQUE_INT(set_num_t)
UNIQUE_INT(entry_num_t)
UNIQUE_INT(lru_entry_t)


Macros are cool. It would have been great if Bjarne had improved the
preprocessor as well when he invented the C++ language... perhaps it
is still possible with the new C++ standard coming up?


There are definitely going to be some improvements, because variadics and
several other features from C99 are going to be added. Other features, such as
scoped macros, are under consideration.

Jonathan
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon G Best | last post by:
Hello! The C++ standard library provides facilities for finding out the sizes (and other such stuff) of numeric types (::std::numeric_limits<>, for example). What I would like to do is to...
14
by: David Fisher | last post by:
The most common sizes of integer types seem to be: 8 bits - signed char 16 bits - short 16 or 32 bits - int 32 or 64 bits - long Question #1: Does anyone know how common sizes other than...
8
by: Lash Rambo | last post by:
How does one go about programming in C++ without making assumptions about the size of types? Is it really possible? For instance, even the well-intentioned, portable C++ (and C) code I've seen...
3
by: MLH | last post by:
I have a 50-char job name field (text type) in an Access table. The entries into this field will be unique. No two records will house the same job name. I would like an algorithm that would...
20
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg.,...
1
by: J.J. Feminella | last post by:
I have an unfinished small class hierarchy that looks something like the code snippet below. The intent is to have a simple company identification system that ensures uniqueness by calling...
2
by: Peter Oliphant | last post by:
Sometimes it's hard to get straight when passing or storing or returning an instance of a class whether you are still dealing with the original object or a copy. For example, the '=' operator used...
159
by: Bob Timpkinson | last post by:
Hi, I have a 32-bit machine... Is there anyway I can get gcc to use the following integer sizes? char: 8 bits short: 16 bits int: 32 bits long: 64 bits long long: 128 bits
130
by: euler70 | last post by:
char and unsigned char have specific purposes: char is useful for representing characters of the basic execution character set and unsigned char is useful for representing the values of individual...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.