
This code is extremely simple, but it begs several questions, and I hope this forum is appropriate for discussion: 1. Is there something similar in boost already? 2. Are nested anonymous namespaces portable across most common implementations? 3. Does anyone know of a better way of doing this, especially overcoming the problem with having multiple declarations on the same line? 4. Does anyone know of a way to create a unique object id constant that does not require something like using the address of a static variable -- this would require memory for each instance of the object id? 5. Any other comments about this snippet, or related issues? Thanks!! #ifndef wjh__unique_type__hpp_ #define wjh__unique_type__hpp_ // Some of my meta-programming tasks require a completely // unique type. I am sure there is a better way, but this // is what I currently have. // // Simply use the macro WJH_UNIQUE_TYPE in any source file, // and a unique type will be defined. // // This depends on: // __LINE__ providing the line number of the source from which // the macro is called. // // The nested anonymous namespace is specific to the compilation // unit. So, each unit can have a type created with the same // line number, but the two types will still be unique. // // Note that it "breaks" if you put more than one WJH_UNIQUE_TYPE // on a single line of code. // // I tried to create an OID at compile time, but was unsuccessful. // This would certainly make the job of creating a unique type // a lot easier. // // I almost always use the macro directly: // wjh::typemap<std::string, WJH_UNIQUE_TYPE> // but there may be reasons for: // typedef WJH_UNIQUE_TYPE type_x; namespace wjh { namespace detail { namespace { template <int v> struct unique_type_helper { }; } // end anonymous namespace } // end detail namespace } // end wjh namespace // Construct a "unique" type #ifdef WJH_UNIQUE_TYPE #warning WJH_UNIQUE_TYPE is already defined! #else #define WJH_UNIQUE_TYPE wjh::detail::unique_type_helper< __LINE__ > #endif #endif // wjh__unique_type__hpp_ -- Jody Hagins Some men are born mediocre, some men achieve mediocrity, and some men have mediocrity thrust upon them. -- Joseph Heller, "Catch-22"