473,837 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RFC: Enum aggregation

I have been working on some project recently that uses lots of enums with
disjunctive intervals of values because it is rather convenient way to
define series of constants with consecutive values. The problem is that some
functions should only accept some of the ranges. To store values of more
than one range/enum in variable one has to use int as storage which imho
compromises type safety because any function that wants to accept such
union/range has to use int as paramter to pass these values. Thinking about
this led me to "enum aggregation" which would basically would be this:
Enum AB has all enumerators from A and B with their respective values and it
adds its two enumerators to the set. Values of AB's own enumerators start at
max(A,B)+1 unless specified otherwise.

enum A {E1, E2};
enum B {E3, E4};
enum AB : A, B {E5, E6};
// AAB and ABB are different types but they have same range of values.
enum AAB : A, A, B {};
enum ABB : A, B, B {};
enum C : A {E1 = 1}; // Ill-formed, redefinition of E1 from enum
A a;
B b;
AB c;

// Ordinary assignment to variable of enum A type.
a = E1;

// Assignment of from base enum. Should work. Implicit cast to derived type.
c = a;

// Assignment from derived enum to base.
a = static_cast<A>( c); // Ok with cast.
b = c; // Ill-formed, requires cast.Overloadin g resolution
Enumerators always have type of the enum in that they are declared.

void f (A a); // 1
void f (B b); // 2
void f (AB ab); // 3
void g (AAB);
void g (ABB);

f (a); // Calls 1 as the types match exactly.
f (c); // Calls 3 as the types match exactly too.
f (E3); // Calls 2 because E3 is defined in enum B.
f (static_cast<AB >(E3)); // Calls 3.
g (E1); // ill-formed, ambiguous resolution.Temp lates
Because enumerators and variables have exact type, no ambiguities, template
parameter deduction should always deduce correct types. Unless of course
pathological case as with function g() appears.
Vaclav Haisman

PS: While I was witing this post it has occurred to me that solving this
problem using Ada-like subtypes isn't bad idea either.

Jul 22 '05 #1
0 1751

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

Similar topics

by: Nice Chap | last post by:
Aggregation in COM was defined as 'Exposing an interface of an inner object by the outer object as though it were an interface of the outer object'. Is this type of aggregation possible in c#? For example... Class COuter - 'Contains' an instance variable of type CInner Class CInner - Implements IList Can COuter be made to look like its implementing IList?
by: John Wood | last post by:
Let's say you're provided with an instance of a class. The instantiation takes place in another module that you have no control over. However, you've extended that class with your own value-added functionality. In C#, given such an instance, and a derived class, there's no way to 'attach' the instance to the class -- you have to either change the way the class was instantiated (not possible in this case), or wrap the class and delegate...
by: cmrchs | last post by:
Hi, how do I implement aggregation and how composition in C# ? When I say : an Airplane has a Pilot then I use aggregation but when I say : an Airplane has a Cockpit then I use composition. How do I implement the difference in C# ? Here's what I try : class Pilot
by: Jozsef Bekes | last post by:
Hi, I would like to implement aggregation in C#, therefore I'd need to implement the queryinterface COM function of a class. I am not sure whether this can be done, and if yes where to start. If someone has an example, a hint or anything that might help, please share it with me. Thanks and Regards, Jozsi
by: Frederik Vanderhaegen | last post by:
Hi, Can anyone explain me the difference between aggregation and composition? I know that they both are "whole-part" relationships and that composition parts are destroyed when the composition whole is destroyed. Under a "whole-part" relationship I understand the following: the whole can't exists without the parts, but can the parts exist without the hole? f.e.: a car can't exist without an engine private engine _Engine
by: SenthilVel | last post by:
Hi Can any one let me know the websites/Pdf for learning Aggragation in C#?? Thanks Senthil
by: ninjutsu28 | last post by:
hi im juz a student so pls bear with my terminologies..juz wana ask how to perform aggregation in vb.net code.. in my model, i have rectangle, shape, drawing classes..rectangle class inherits shape class (is - a relationship), and drawing class has shape class(aggregation) Shape Class: Public MustInherit Class CShape Rectangle Class: Public Class CRectangle
by: Karigar | last post by:
I have been so far developing COM servers and clients in C++. I am new to C#/NET way of doing COM and was wondering if it is possible to accomplish aggregation in .NET platform. By aggregation I mean classic COM aggregation in which the outer object controls which interfaces it of inner object it exposes, and to the outside world it appears as the inner object without having to implement all the interfaces. By aggregation I do not mean a...
by: Jeff | last post by:
hey Can OO Aggregation be described as: - A system of objects that are built using each other any comments? Jeff
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...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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...
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,...
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...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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.