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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:07:07+00:00 2026-05-27T23:07:07+00:00

So I saw code like this: template<class T> T* safe_ptr_cast(Message& msg) { assert(msg.header()->size() ==

  • 0

So I saw code like this:

template<class T>
T* safe_ptr_cast(Message& msg) {
    assert(msg.header()->size() == T::size());
    return msg.header()->size() == T::size ? reinterpret_cast<T*>(msg.dest()) : NULL;
}

How can the msg be cast? Generally, the parameter list needs to have a parameter type T in order to let the function recognize what type to cast. However, in this case, the parameter type is explicitly stated. Does this mean reinterpret_cast will cast the Message type to its corresponding subclasses?

For example, the Message class has a subclass RequestMessage with some additional members, a subclass ResponseMessage with some additional members as well. I assume the size of RequestMessage is 50 bytes, and ResponseMessage is 100 bytes. Upon casting, if the msg object has 50 bytes, it will be cast to RequestMessage, and if the msg object has 100 bytes, it will be cast to ResponseMessage. Is this correct?

  • 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-27T23:07:07+00:00Added an answer on May 27, 2026 at 11:07 pm

    I’m not totally sure I understand the question, but I think you’re asking how does the compiler know what type you want to cast to, because the parameter is always a Message&

    It does not automagically select based on the size: the assert is there to catch programmer errors.

    You have to manually specify what type you want when you call the function: safe_ptr_cast<RequestMessage>(msg)

    EDIT: I had meant to comment that you may want to look at virtual functions because they might be more appropriate (at least in one situation I can imagine where you’d have a function like this one).

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

Sidebar

Related Questions

I saw the Qt source code like this: class Q_CORE_EXPORT QBasicAtomicInt { public: ...
I saw somewhere code like this: return View(x=>x.List()); Instead of return View(List); What do
I saw someone writing code like this , in a C++ class: int foo
In some code, I saw singleton template template<typename T> class Singleton { public: Singleton(T&
I saw some code like this: try { db.store(mydata); } finally { db.cleanup(); }
I recently saw a bit of code that looked like this (with sock being
I saw a piece of code which was written like this: if (from n
I recently saw code that looks like this: (function (someGlobal) { someGlobal.DoSomething(); })(someGlobal); where
I saw some code written by another developer that looks something like this: var
Just now,I saw some code like this: if(condition){ var xx='sss'; } //do something if(condition){

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.