Unique Integer Types -- a useful C++ trick

Discussion in 'C++' started by Christopher Brian Colohan, Nov 19, 2004.

  1. 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: PGP: finger
    Web: www.colohan.com Phone: (412)268-4751
     
    Christopher Brian Colohan, Nov 19, 2004
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Nov 19, 2004
    #2
    1. Advertising

  3. "Victor Bazarov" <> wrote in message
    news:O9tnd.11193$09.us.to.verio.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
     
    Jonathan Turkanis, Nov 19, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karl Seguin

    Trick for unique row id when binding

    Karl Seguin, Oct 27, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    361
    Karl Seguin
    Oct 27, 2004
  2. MG
    Replies:
    5
    Views:
    445
    Christian Bau
    Jul 22, 2003
  3. Robert W Hand

    Re: integer divide by 7 trick?

    Robert W Hand, Jul 20, 2003, in forum: C++
    Replies:
    12
    Views:
    708
    Paul Hsieh
    Jul 25, 2003
  4. JS
    Replies:
    1
    Views:
    334
    Peter van Merkerk
    Jul 22, 2003
  5. Replies:
    163
    Views:
    3,344
Loading...

Share This Page