473,473 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Yves Meynard's Enumeration class (CUJ Web exclusive, May 2003)

This all pertains to the above mentioned article. Anyone want to read it
(it's rather neat, actually), here's the address:

http://www.cuj.com/documents/s=8205/cujweb0305meynard/

Anyway, I was trying to work my way through the idea of the class,
especially the one, that uses a template parameter for the enumeration
value, in the hopes of getting an enum that enumerated through std::strings.

The whole idea is to keep a base class with a static std::set<> of
pointers to static instantiations of a derived class. The whole shebang
designed using the Curiously Recurring Template Pattern (meaning that
the derived class passes itself as a template parameter to the base
class). Here's a simplified implementation (read the above link for full
details.

<code>

template <class T, class TValue>
class Enum
{
private:
// Actually needs a "less-than" predicate. Omitted here...
static std::set<Enum<T, TValue> *> s_instances;
TValue m_value;

protected:
explicit Enum(TValue const & value): m_value(value)
{
s_instances.insert(this);
}

public:
TValue const & get_value() const { return m_value; }

// ... more static functions follow to implement an STL-like
// interface to the enum; begin(), end(), size(), et al.
};

// instantiate the s_instances std::set<>
template <class T, class TValue>
typename std::set<Enum<T, TValue> *>
Enum<T, TValue>::s_instances;

// An actual "enum", enumerated on std::string
class Enum_String
: Enum<Enum_String, std::string> // <- Curiously Recurring Template
{
private:
explicit Enum_String(std::string const & value)
: Enum<Enum_String, std::string>(value){} // Base class "inserts"

public:
static const Enum_String Alpha;
static const Enum_String Beta;
// ... however many _valid_ values of Enum_String is needed...
};

// Instantiate the static "Enum_String"s
const Enum_String Enum_String::Alpha("Alpha");
const Enum_String Enum_String::Beta ("Beta");

</code>

Here's the problem. At the above instantiations, an anonymous
std::string ("Alpha") is constructed and passed to Enum_String's
constructor. (Pseudo-code)

Enum_String::Enum_String(std::string("Alpha"))

This constructor, in turn, passes the value to the instantiated base
class' constructor.

Enum<Enum_String, std::string>::Enum(std::string("Alpha"))

This constructor set the m_value member variable *and inserts a pointer
to this into the set*!!! Or at least it tries to.

Problem is, AFAIK, C++ does not guarantee that the s_instances variable
has been constructed at this time. Both s_instances and the
"Enum_String"s are constructed globally, but I believe that there is no
guarantee that s_instances is constructed first, making the construction
of Enum_String impossible (since the constructor relies on s_instances).

If I understand C++ rules correctly, this may work or not, as "the wind
blows", depending on whether the compiler decides to instantiate
s_instances first or not.

Now my question is: Is there a way to get around this problem, ie. make
sure that s_instances is constructed *before* any of the Enum_String
instances?

Or have I misunderstood completely? (always a possibility! :) )

Opinions and comments, please! :)

--
/Brian Riis
Jul 22 '05 #1
0 1304

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

Similar topics

1
by: Grandma Wilkerson | last post by:
Hi, The documentation states that enumeration through a collection is inherently NOT thread-safe, since a thread which added/removed an item from said collection could screw up the thread that...
16
by: NPC | last post by:
Hi, Is anyone else seeing a pattern with the frequency of Microsoft-related C++ articles (not including advertisements) appearing in the 'C/C++ Users Journal' over the last 6 months? It appears...
1
by: Sergey Poberezovskiy | last post by:
Hi, I have a simple enumeration in my schema: <xs:element name="el_1"> <xs:simpleType> <xs:restiction base="xs:string"> <xs:enumeration value="value and space 1"/> <xs:enumeration...
0
by: Wayne | last post by:
Can someone please help with this problem. When referring to opening a database in Exclusive mode the Access 2003 help says: "Under Default open mode, do one of the following: If you want others...
4
by: Marshal | last post by:
Sure... IEnumerable was inconvenient suggesting a separate class to service the enumeration, IEnumerator, and multiple operations: Current, MoveNext, Reset. (I'll warp the definition of "operation"...
3
by: Sampson | last post by:
I have a question about enumeration and how to populate them during runtime. I am using vb.net but will happily take any advice in c# as well. Here is an example to help illustrate what I am...
3
by: | last post by:
Can any one who has written an manuscript for CUJ please share your experiences? In particular I'd like to hear from you if your manuscript was rejected for some reason rather than being...
26
by: Eric B | last post by:
You will be missed.
3
by: scotchfx | last post by:
Is there any way of declaring an enumeration in a base class in such a way that it can be "overridden" in a derived class... for instance: class Base{ public: enum Enum { //empty }; } ...
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
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.