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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:58:28+00:00 2026-06-07T15:58:28+00:00

How can I downcast safely (ie returning null on failure) to the exact type

  • 0

How can I downcast safely (ie returning null on failure) to the exact type of the underlying object, without incurring the performance penalty of dynamic_cast, and without having to put support code in every class I use?

  • 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-07T15:58:30+00:00Added an answer on June 7, 2026 at 3:58 pm

    dynamic_cast will traverse the entire inheritance tree to see if the conversion you want is possible. If all you want is a direct downcast to the same type as the object, and you don’t need the ability to cross cast, to cast across virtual inheritance, or to cast to a base class of the object’s actual type, the following code will work:

    template<class To>
    struct exact_cast
    {
        To result;
    
        template<class From>
        exact_cast(From* from)
        {
            if (typeid(typename std::remove_pointer<To>::type) == typeid(*from))
                result = static_cast<To>(from);
            else
                result = 0;
        }
    
        operator To() const
        {
            return result;
        }
    };
    

    The semantics are exactly the same as for other cast operators, ie

    Base* b = new Derived();
    Derived* d = exact_cast<Derived*>(b);
    

    Edit: I have tested this on a project I am working on. My results from QueryPerformanceCounter are:
    dynamic_cast: 83,024,197
    exact_cast:78,366,879
    Which is a 5.6% speedup. This is for non-trivial CPU-bound code. (It does no I/O)

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

Sidebar

Related Questions

Yes, I know that downcast using dynamic_cast can't compile if the Base is not
I can't figure-out how to get at the underlying object in a form_for or
I want to force a downcast on a object what can't be down casted
I learned that I can use the real type of a Object to define
You can upcast or downcast an instance (to a superclass or subclass) using this
How can improve this sort method to meet the following conditions: exact matches returned
How can I downcast a list of objects so that each of the objects
In this post , a brave wants (in C++) to downcast a object of
Why can I only upcast a generic and not downcast it? How is it
I was reading on this page and it says dynamic_cast can be used only

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.