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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:29:19+00:00 2026-05-18T09:29:19+00:00

Long ago I have created a following template so that I get an assert

  • 0

Long ago I have created a following template so that I get an assert whenever I perform a static_cast but the type is not what I assume it to be:

/// perform a static_cast asserted by a dynamic_cast
template <class Type, class SourceType>
Type static_cast_checked(SourceType item)
{
  Assert(!item || dynamic_cast<Type>(item));
  return static_cast<Type>(item);
}

Today I wanted to create a variant which would work not only with pointers, but with references as well:

/// overload for reference
template <class Type, class SourceType>
Type &static_cast_checked(SourceType &item)
{
  Assert(dynamic_cast<Type *>(&item));
  return static_cast<Type>(item);
}

However, the compiler does not seem to use this overload when I am casting a reference to another reference. I am afraid I do not understand template resolution rules enough to understand why, or to be able to create a variant which works.

Note: I cannot catch the bad_cast exception instead of checking dynamic_cast<Type *> for NULL, as exceptions are disabled for this project.

  • 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-18T09:29:20+00:00Added an answer on May 18, 2026 at 9:29 am

    Remove * and & from the return types:

    /// perform a static_cast asserted by a dynamic_cast 
    template <class Type, class SourceType> 
    Type static_cast_checked(SourceType *item) 
    { 
      Assert(!item || dynamic_cast<Type>(item)); 
      return static_cast<Type>(item); 
    } 
    
    template <class Type> struct make_pointer
    {
        typedef Type *PointerType;
    };
    
    template <class Type> struct make_pointer<Type &>
    {
        typedef Type *PointerType;
    };
    
    /// overload for reference 
    template <class Type, class SourceType> 
    Type static_cast_checked(SourceType &item) 
    { 
      Assert(dynamic_cast<typename make_pointer<Type>::PointerType>(&item)); 
      return static_cast<Type>(item); 
    } 
    

    Then you can use your desired syntax:

    Derived *d= static_cast_checked<Derived *>(b);
    Derived &d= static_cast_checked<Derived &>(b);
    

    EDIT: added a pointer type conversion.

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

Sidebar

Related Questions

For a long time ago, I have thought that, in java, reversing the domain
Not too long ago, I had a problem which required me to set WinDbg.exe
A long time ago I had the following directory structure in my SVN repository
Long ago I tried to sort out my system between local, web server and
long, long ago in a galaxy far far away, You used to be able
[The only similar question I can find was answered long ago and contains a
Long time ago, the Game Developer magazine published an article about ID Software and
A long time ago I had an apple ][ . I remember the command
A long time ago when I was a young lat I used to do
A long time ago I saw this trick in Ruby. Instead of doing (for

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.