473,796 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
+ 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.ins ert(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_inst ances;

// An actual "enum", enumerated on std::string
class Enum_String
: Enum<Enum_Strin g, std::string> // <- Curiously Recurring Template
{
private:
explicit Enum_String(std ::string const & value)
: Enum<Enum_Strin g, std::string>(va lue){} // 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_Strin g"s
const Enum_String Enum_String::Al pha("Alpha");
const Enum_String Enum_String::Be ta ("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::En um_String(std:: string("Alpha") )

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

Enum<Enum_Strin g, std::string>::E num(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_Strin g"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 1335

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

Similar topics

1
2099
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 was iterating. That makes sense.. but... given a collection that is filled at start-time and never modified again, is it safe to have multiple threads *reading* (not writing to) the collection using foreach()? I have a class that exposes an...
16
327
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 that many of the usual columnists are pushing hard to have the C++ Standard follow several new inventions in the Managed C++ World. Mostly, these new inventions appear necessary to solve issues .NET is having with using only standard C++. ...
1
3669
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 value="value2 with spaces"/> ...
0
1991
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 to be able to open the Microsoft Access database at the same time you have it open, click Shared. If you want sole access to the Access database when you have it open, click Exclusive." >From my experience and from what I have read on the...
4
5664
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" for a second if you don't mind). However, it existed within intuitive language semantics, whereas the new "yield" keyword, while highly convenient, is also one of the most gross warping of language concepts to date... public IEnumerator...
3
10630
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 after. Create a class named “clsMyItems” and in that class place an enum. Public Enum Items Item1 = 0 Item2 = 1 Item3 = 2
3
1291
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 published. The reason I ask is because earlier this year I submitted a proposal for CUJ. Early email communication with CUJ went well, I got an immediate reply from my proposal and told to go ahead and write the manuscript.
26
1932
by: Eric B | last post by:
You will be missed.
3
13753
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 }; } class Derived : public Base{
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10449
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9047
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5440
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4114
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 we have to send another system
3
2924
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.