Click here to Skip to main content
12,951,289 members (48,592 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 4 Jun 2001

An unique value template

, 11 Sep 2001
Rate this:
Please Sign up or sign in to vote.
Maintain unique ID for every object with no memory overhead


The purpose of this class is :-

  • Defines template for automatic sequential ID numbers.
  • Useful for implementing IDs.
  • Supports multiple unique ID categories.
  • The values are guaranteed to be sequential in each category.


template <typename TValue, typename TCategory>
class unique_value
    typedef TValue value_type;
    typedef TCategory category_type;

    // default constructor

    // cast to value_type operator
    operator const value_type&() const;

    // read acessor for the unique counter
    static const value_type& get_counter();

    value_type m_value; //the unique value


  • Use as a numeric data type in the class you want to have unique IDs.
  • The category is determined of both TValue and TCategory.
  • It's recommended to create a typedef and to use it for all your IDs unless you have a reason not to do so.
  • Use INIT_UNIQUE_CAT and INIT_UNIQUE macros to initialize categories
typedef unique_value<long> unique_long;

class some_data_class
    unique_long ID; // unique for every object
    int data1;


    int dataN;

class different_category_data_class;
INIT_UNIQUE_CAT(long, different_category_data_class);

class different_category_data_class
    unique_value<long, different_category_data_class> ID;


    some_data_class data_object; 
    different_category_data_class another_object;
    cout << data_object.ID << another_object.ID; 

Code listing

#if !defined(__UNIQUE_VALUE__)
#define __UNIQUE_VALUE__

typedef int default_unique_category;

template <typename TValue, typename TCategory=default_unique_category>
class unique_value
    typedef TValue      value_type;
    typedef TCategory   category_type;

    // default constructor

    // cast to value_type operator
    operator const value_type & () const {return m_value;}; 

    // read acessor for the unique counter
    static const value_type& get_counter() {return m_counter;}

    value_type m_value;             //the unique value

    unique_value   (value_type&);   // FORBIDEN assignment constructor
    void operator= (value_type&);   // FORBIDEN assignment operator

    // static variable used for unique value generation
    static value_type m_counter;    

// static members initialization macros
// call before using the template, once per template instance
#define INIT_UNIQUE(t_value) \
    INIT_UNIQUE_CAT(t_value, default_unique_category)

#define INIT_UNIQUE_CAT(t_value, t_category) \
    unique_value<t_value, t_category>::value_type \ 



  • Created - 6 May 2001
  • Added multithreading support - 6 June 2001
  • Removed multithreading support - 12 Sept 2001


I'd be glad if somebody uses this class. If you do, please mail me at I had to remove multithreading support. The reason is that you can't achieve platform dependent behaviour using platform independent ways. If you still need multithreading - see the discussion below.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Bulgaria Bulgaria
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMultithreading Pin
LOXmith5-Jun-01 6:11
memberLOXmith5-Jun-01 6:11 
GeneralDIY :) Pin
LOXmith6-Jun-01 0:38
memberLOXmith6-Jun-01 0:38 
GeneralRe: DIY :) Pin
ansont6-Jun-01 22:17
memberansont6-Jun-01 22:17 
GeneralRe: DIY :) Pin
LOXmith6-Jun-01 22:37
memberLOXmith6-Jun-01 22:37 
GeneralRe: DIY :) Pin
James R. Twine7-Jun-01 10:19
memberJames R. Twine7-Jun-01 10:19 
GeneralRe: DIY :) Pin
LOXmith7-Jun-01 21:00
memberLOXmith7-Jun-01 21:00 
GeneralRe: DIY :) Pin
James R. Twine8-Jun-01 3:57
memberJames R. Twine8-Jun-01 3:57 
GeneralRe: DIY :) Pin
LOXmith8-Jun-01 4:40
memberLOXmith8-Jun-01 4:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170525.1 | Last Updated 12 Sep 2001
Article Copyright 2001 by LOXmith
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid