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 3306468
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:17:31+00:00 2026-05-17T21:17:31+00:00

There are a huge feature collection in C++ programming language that supply a strict

  • 0

There are a huge feature collection in C++ programming language that supply a strict control over datatypes. Frequently one would mold his code with template system to achieve the most adequate functionality while guaranteeing within it correct type preservation and flexibility in its manipulation. Less frequently enumerations are used for this purpose, since they permit define an explicit possibilities for the data and have no direct way to verify type correctness.

So what we have are templates that allow functions and classes to operate with generic types without being rewritten for each one; and enums, providing a straight use of the expected types.

Is it possible to define a restricted template architecture for a class using an enum as a model? How can a templatized method use enumed type and when it could be useful?

My final question is: how to conjoin templates and enumeration types and use them together in the abstract data design?

As an example suppose you are trying to create a data container you expect to have types defined in an enum within your class:

template <typename T>
class element
{
public:
  enum type { text_type, widget_type, image_type };

  ...
private:
  type node_type;

};

You also defined each of the types present in class element:

class text;
class widget;
class image;

Creating an element, you want to specify its content type (text, widget or image) so you pass it by template argument.

if (node_type == text_type) do_something();

In Java you have List<? extends T> that implicitly says what type of classes can be used as template parameter. Can it be done in C++ using enums? How to work with enumed types in statements?

Question: I’d like to know how to restrict the behavior of templates by enumed types to ensure only these are being used, and orient the code using traditional way of working with enums.

  • 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-05-17T21:17:31+00:00Added an answer on May 17, 2026 at 9:17 pm

    The type traits idiom as @UncleBens illustrates is the usual way of solving this problem.

    You can attach information to classes using static const members of integer or enumeration type, as well.

    #include <iostream>
    
    enum color { red, green, blue };
    
    struct x {
        static const color c = red;
    };
    
    template< color c >
    struct thing;
    
    template<>
    struct thing< red > {
        thing() { std::cout << "red\n"; }
    };
    
    int main() {
        thing< x::c >();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two processes one will query other for data.There will be huge amount
Here is the problem I'm running into. There's a huge legacy application that runs
I just read up on a performance of LINQ, and there is a HUGE
I am include a huge javascript file(500K) in my HTML. Is there a smart
There is a conversion process that is needed when migrating Visual Studio 2005 web
So I'm currently working on a project that involves the collection and storing of
In many reviews, there's usually a feature to sort by date, helpfulness, etc... Essentially
There are two weird operators in C#: the true operator the false operator If
There are two popular closure styles in javascript. The first I call anonymous constructor
There is previous little on the google on this subject other than people asking

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.