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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:13:37+00:00 2026-05-16T18:13:37+00:00

I saw below thing about switch statement in c++ standard $6.4.2. Switch statement can

  • 0

I saw below thing about switch statement in c++ standard $6.4.2.

Switch statement can take a condition.

The condition shall be of integral type, enumeration type, or of a class type for which a single conversion function to
integral or enumeration type exists (12.3). If the condition is of class type, the condition is converted by calling that
conversion function, and the result of the conversion is used in place of the original condition for the remainder of this
section

I tried below code which is working fine.

class Test
{
public:
    operator int() { return 1; }
};

int main()
{
     Test obj;
     switch(obj)
     {
        case 1: cout<<"Test class object";
        break;
     }
}

Is this a better way compared to using a typeid operator to find the object type ?

In switch case way, the overhead is each class should have a unique integer id which will be returned by conversion function.

In typeid way, if we use like typeid(obj) == typeid(Test), if else chain will be lengthy when we have many class types. Code readability decreases. May be its slower as well compared to switch case, as switch case may be implemented like a jump table by Compiler

So, which way is better to find the object type at runtime ?

EDIT: corrected question considering Andrey’s comments.

  • 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-16T18:13:38+00:00Added an answer on May 16, 2026 at 6:13 pm

    Where did you get the idea that “string comparison has to be performed”? In order to determine if two type_info objects designate the same type, you need to compare these type_info objects directly, as in typeid(obj) == typeid(Test).

    In fact, you cannot do the same things by comparing the strings returned by type_info::name() member simply because the language makes no guarantees about these strings at all. More specifically, it makes no guarantees about the uniqueness of these strings for each given type. They all can return "Hello World!" for all types. Or they can return an empty string for all types. Usually the implementations behave nicer than that, but in any case the name() member is there for some potential debugging/informational purposes. You cannot meaningfully rely on type_info::name() in the actual functionality of your code.

    Also, the language standard says that type_info objects are lvalue objects with static storage duration. I’d expect these type_info object to maintain their “address identity” for each specific type (although I’m not sure the standard actually guarantees that). I.e. I’d expect that &typeid(type) == &typeid(type) is always true (i.e. every time you invoke typeid for the same type, you get the same lvalue as the result). If so, you can compare the addresses of type_info objects instead of comparing them using the == operator. You can also use the addresses to build some more complicated data structure for type matching, like an associative array. You can’t use switch/case with it though. (And again, I’m not sure my assumption about stable address identity of type_info is valid). In fact it is not. As Johannes Schaub noted in the comments, the correct way to put type_infos into an ordered container is to use the type_info::before() to establish ordering.

    Of course, in your specific case using a manually implemented integer identifier for the class (if you really really need to go that way) might be much more efficient. It also allows you a greater flexibility to implement your intent better. For example, typeid(Test) == typeid(const Test) will evaluate to false, which might not be what you want. However, hijacking the conversion operator to such a basic type as int for that purpose is definitely not a good idea. If you really need it, make it a named method and return something named, not a “magic constant”.

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

Sidebar

Related Questions

I saw below thing in c++ standard (§9.5/1): A union shall not have base
I'm reading a book about SQL. In that book, I saw strange query below:
I saw the below statement in Java Specifications. Programs where threads hold (directly or
I have posted an example below and the desired result . I saw a
i saw a example for login form same blow code class Form_Login extends Zend_Form
I saw that even by writing separate commands, we can combine two different types
I saw many threads with this tittle, but no one really speak about reuse
I have an activity defined as below: <activity android:name=com.example.ui.HomeActivity android:alwaysRetainTaskState=true/> A strange thing is
Comparing the HashMap and Hashtable source code in JDK 1.6, I saw the below
In Spring web mvc 1) If we define DispatcherServlet as below <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

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.