473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Enum to type (for static dispatching)

I was thinking in a way to do static dispatching on enumerations, in a
way similar to dispatching on integral constants using Loki's
Int2Type<> or Boost.MPL's int_<>, i.e, creating types based on the
enumeration constants, so I came up with this example code:

#include <iostream>

using namespace std;

template <class EnumType>
struct enum_ {
typedef enum_<EnumType> type;
typedef EnumType value_type;

};

template <class EnumType, EnumType EnumValue>
struct enumerator_ : public enum_<EnumType> {
typedef typename enumerator_<EnumType, EnumValue> type;
typedef typename enum_<EnumType>::value_type value_type;
enum {value = EnumValue};
operator EnumType() const { return EnumValue; }

};

namespace f {
enum Fruit { Apple, Banana, Cherry };

typedef enumerator_<Fruit, Apple> AppleType;
typedef enumerator_<Fruit, Banana> BananaType;
typedef enumerator_<Fruit, Cherry> CherryType;

// helper constants
static const AppleType AppleTag;
static const BananaType BananaTag;
static const CherryType CherryTag;

// a helper -- we don't have template typedef yet
template <Fruit F> struct tag : public enumerator_<Fruit, F>
{};

};

void Eat(f::AppleType, int number) {
cout << "Eating " << number << " apples." << endl;

}

void Eat(f::BananaType, int dozens) {
cout << "Eating " << 12 * dozens << " bananas!" << endl;

}

void Eat(f::CherryType, int number) {
cout << "Eating " << number << " cherries." << endl;

}

int main()
{
Eat(f::tag<f::Apple>(), 3);
Eat(enumerator_<f::Fruit, f::Banana>(), 2);
Eat(f::CherryTag, 10);

return 0;

}

Output is:
Eating 3 apples.
Eating 24 bananas!
Eating 10 cherries.

Of course this is a silly example and not so useful, but it would be
more useful in situations involving template parameters or static
dispatching to constructors.

One of the problems in this solution is that it involves a lot of
repetition (each enumerator name was declared three times.. e.g, Apple,
AppleType and AppleTag).

What do you think of this solution? Is it 'legal'? How can it be
improved?

Jan 26 '06 #1
0 1607

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

Similar topics

3
by: Ajax Chelsea | last post by:
can not the "static const int" be replaced by "static enum" anywhere? is it necessary that define special initialization syntax for "static const int"?
9
by: AngleWyrm | last post by:
"The C++ Programming Language" by Bjarne Stroustrup, copyright 1997 by AT&T, section 4.8 (pp 77): "A value of integral type may be explicitly converted to an enumeration type. The result of such a...
12
by: Steven T. Hatton | last post by:
Any opinions or comments on the following? I don't say it below, but I came out on the side of using enumerations over static constants. /* I'm trying to figure out the pros and cons of using...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
6
by: | last post by:
I'm new to VS.NET, C#, and the enumerated datatype. I'm told that VS.NET 2005 Intellisense will pop up the members of an enum as a selection list if you are using the enum as a method parameter....
13
by: toton | last post by:
Hi, I have some enum (enumeration ) defined in some namespace, not inside class. How to use the enum constant's in some other namespace without using the whole namespace. To say in little...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
3
by: hufaunder | last post by:
Imagine you have a charting library that can draw lines, bars, floating bars, bands, etc. Lines and bars need only one input. Floating bars and bands need two inputs. There are two approaches: ...
2
by: Daniel Jeffrey | last post by:
Hello. Can anyone help me please. I have created an Enumeration, and I loop through it, it all works ok, except the first item returns 2 times. Code is below. When called I Get "Text Edit" 2...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...

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.