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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:25:50+00:00 2026-06-07T09:25:50+00:00

I’m trying to implement std::is_enum . Here is my code so far: template<typename T>

  • 0

I’m trying to implement std::is_enum. Here is my code so far:

template<typename T>
struct is_enum {
    static bool value;
};

template<typename T>
bool is_enum<T>::value = false;

template<enum E>
struct is_enum {
    static bool value;
};

template<enum E>
bool is_enum<E>::value = true;

This code causes error. More precisely:

g++ -std=c++0x -Wall -o "enum2" "enum2.cpp" (in directory: /home/aristophanes/Desktop/C++)
Compilation failed.
enum2.cpp:11:15: error: use of enum ‘E’ without previous declaration
enum2.cpp:3:10: error: template parameter ‘class T’
enum2.cpp:12:8: error: redeclared here as ‘int E’
enum2.cpp:16:15: error: use of enum ‘E’ without previous declaration
enum2.cpp:17:14: error: ‘E’ was not declared in this scope
enum2.cpp:17:15: error: template argument 1 is invalid
enum2.cpp:17:18: error: template declaration of ‘bool value’

Can anyone explain to me where I make a mistake? It is mine or the compiler’s fault? Thanks in advance.

Edit: if it is completely wrong, then how can I correct it?

Note: I’m using g++ -o <file> <file>.cpp

  • 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-07T09:25:53+00:00Added an answer on June 7, 2026 at 9:25 am

    The best way to implement this is to use compiler magic, and I believe most implementations do this.

    For example, here’s libc++’s implementation for gcc >= 4.3 and any compiler that __has_feature(is_enum)1

    template <class _Tp> struct _LIBCPP_VISIBLE is_enum
        : public integral_constant<bool, __is_enum(_Tp)> {};
    

    For all other compilers libc++ does:

    template <class _Tp> struct _LIBCPP_VISIBLE is_enum
        : public integral_constant<bool, !is_void<_Tp>::value             &&
                                         !is_integral<_Tp>::value         &&
                                         !is_floating_point<_Tp>::value   &&
                                         !is_array<_Tp>::value            &&
                                         !is_pointer<_Tp>::value          &&
                                         !is_reference<_Tp>::value        &&
                                         !is_member_pointer<_Tp>::value   &&
                                         !is_union<_Tp>::value            &&
                                         !is_class<_Tp>::value            &&
                                         !is_function<_Tp>::value         > {};
    

    Some of those other type traits still require compiler magic.2 E.g. is_union. However, that condition can be rewritten such that it doesn’t need compiler magic. This can be done by replacing the seperate checks for unions and classes with a single check for both, as Johannes Schaub points out.

    1. So far as I know only clang implements __has_feature, unfortunately.
    2. It’s interesting that libc++ does have a version of is_union<T> and is_class<T> that do not use compiler intrinsics, but as a result they provide erroneous results for union types. But their erroneous results are complementary so libc++’s fallback implementation of is_enum<T> provides accurate results.

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

Sidebar

Related Questions

public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.