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

  • Home
  • SEARCH
  • 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 6689771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:33:30+00:00 2026-05-26T05:33:30+00:00

This code compiles . Its obviously wrong because B can never be a WTF.

  • 0

This code compiles. Its obviously wrong because B can never be a WTF. Is there a way i can write the typecast so this is a compile time error?

class B{ public: virtual void abc(){} };
class D1 : public B{};
class WTF{ };

template<class T, class TT>
T DoSomething(TT o){
        return dynamic_cast<T>(o);
}
B*Factory() { return new D1; }
int main(){
        DoSomething<D1*, B*>(Factory());
        DoSomething<WTF*, B*>(Factory());
}
  • 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-26T05:33:30+00:00Added an answer on May 26, 2026 at 5:33 am

    There are two solutions to this problem, depending on what you want the code to be doing.

    The first solution: Change the dynamic_cast into static_cast. This solution assumes that you never use class WTF in a class hierarchy with multiple inheritance. See below.

    The second solution: You realize that C++ allows multiple inheritance. There is no way for you nor for the compiler to predict whether an arbitrary instance of WTF created sometime in the future will inherit from class B or not.

    #include <stdio.h>
    
    class B{ public: virtual void abc(){} };
    class D1 : public B{};
    class WTF{ };
    class WTF_B : public WTF, public B {};
    
    template<class T, class TT>
    T DoSomething(TT o){
        return dynamic_cast<T>(o);
    }
    
    B*Factory1() { return new D1; }
    B*Factory2() { return new WTF_B; }
    
    int main(){
        printf("%p\n", DoSomething<D1*, B*>(Factory1()));
        printf("%p\n", DoSomething<WTF*, B*>(Factory1()));
        printf("%p\n", DoSomething<WTF*, B*>(Factory2()));
        return 0;
    }
    

    The above source code can also be found here. Its output looks like this:

    0x861a008
    (nil)
    0x861a028
    

    In either case, the choice between solution 1 and solution 2 is yours – but the choice is yours only if you know what you are doing.

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

Sidebar

Related Questions

This code compiles: private static void Main(string[] args) { bool? fred = true; if
This code compiles fine: {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances, FlexibleContexts, EmptyDataDecls, ScopedTypeVariables, TypeOperators,
I am a bit confused why this code compiles. I leave out the necessary
I'm having a hard time using std::string::iterators in C++. This code compiles fine (still
I have non-template class with a templatized constructor. This code compiles for me. But
I have this code which compiles and works as expected: class Right {}; class
This piece of code compiles and runs as expected on GCC 3.x and 4.x:
Please check this code out it compiles and runs absolutely fine.. The question is
This code in a GUI application compiles and runs: procedure TForm1.Button1Click(Sender: TObject); begin Self
I saw this code snippet during our lab and it actually compiles in MSVC2008

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.