Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8374647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:58:04+00:00 2026-06-09T14:58:04+00:00

Suppose you’re writing a library, providing a class with some kind of type parameter.

  • 0

Suppose you’re writing a library, providing a class with some kind of “type” parameter. For this one might use an enum:

namespace MyLib {
  class Event {
  public:
    enum EventType { TYPE1, TYPE2, ... };
    Event(EventType _type) : type(_type) { }
  private:
    EventType type;
  }
}

Then to instantiate:

new MyLib::Event(Event::TYPE1);

Fine so far. But what if you want the user to be able to extend the list of event types? This is not possible if the type property is an enum.

Low-quality possibilities include asking them to #define custom event names, or simply using strings, though those both seem suboptimal.

Is there a general solution to this?

One suggestion has been to use a struct EventType to return values within a specified range in an enum. However, while this solves the compiler-safety issue, it doesn’t address the problem of adding named types – it would require the user to add these to the global scope.

One possibility which addresses the latter but not the former issue is to typedef EventType to an integral type, and leave it to the user to add custom types to the library’s namespace or their own. A factory method to provide unique values could be provided as part of Event:

#include <iostream>
#include <vector>

namespace MyLib {

  namespace EventType {
    typedef int T;
    enum { TYPE1, TYPE2, Count};
  }

  class Event {
  public:
    Event(EventType::T _type) : type(_type) { }
    EventType::T type;
    static EventType::T registerType() { return _typeid++; }
  private:
    static int _typeid;
  };

}

MyLib::EventType::T MyLib::Event::_typeid = EventType::Count;

// The user can then add types, including to the library's namespace
// (which may or may not be a good idea)
namespace MyLib { namespace EventType {
  MyLib::EventType::T MYTYPE = MyLib::Event::registerType();
} }

int main() {
  MyLib::Event ev1(MyLib::EventType::TYPE2);
  MyLib::Event ev2(MyLib::EventType::MYTYPE);
  std::cout << ev1.type << std::endl;
  std::cout << ev2.type << std::endl;
  return 0;
}

Outputs:

1
2

While this doesn’t technically restrict the parameter to a registered set of types, the typedef & namespacing provides a helpful syntactic cue in the constructor definition and auto-suggestion in IDEs, and it prevents user types from having to pollute the global scope, which is perhaps the greater concern.

Is there a better way to specify a finite, compiler-checked but user-extensible set of values as a type parameter to a class, or generally to a function/method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:58:05+00:00Added an answer on June 9, 2026 at 2:58 pm

    I think your best bet is to simply take the event type as a normal integral type, use an enumeration to define the built-in types, and provide user-defined min and max values between which the user creates their own integer values with a separate enumerator. Then you’ll just use assertion to make sure that the base event doesn’t try to process an unknown event type.

    EDIT: Do note that a base event class with some types that understands and some that it doesn’t may be fragile, unless it’s just holding the data for the child class to pick out again later. Even in that case it might be better to consider an alternate design. Can you elaborate on how the event type is used after it’s set?

    EDIT2: I think I understand better now, the event stores the event type and an event consumer can retrieve that from the event. If the consumer understands the extended event type it can do appropriate processing, else delegate, assert, or simply no-op. Given that, I think using an integral event type seems fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose both parent and child use one pipe for writing and reading means when
Suppose I'm writing a library or a set of tools mytool where a class
I am writing Plugin for some Application. Lets suppose that Application hava library dependency
Suppose we are writing a class (let's call it Class) in an iPhone program.
This is a follow-up to my previous question . Suppose I am writing the
Suppose, I have a connected socket after writing this code.. if ((sd = accept(socket_d,
This question is for learning purposes. Suppose I am writing a simple SQL admin
Suppose I create a file for writing like this: std::ofstream my_file(filename, std::ios_base::out | std::ios_base::trunc);
Suppose I'm writing a library that stores a sequence of doubles to a file
Suppose I am writing a template library consisting of a function template template<T> void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.