4

On a simple embedded platform I have no RTTI available but I want to use c++ advantages like inheritance for a class hierarchy like the provided sample. At the moment I'm using the following code snipped to simulate a dynamic cast. To simplify this discussion I ported the code to a simple main.cpp. I used the mingw compiler for testing my sample. The code is working as expected but seams not ideal. I'm not searching for a generic dynamic cast replacement solution considering all aspects. Is there any way to implement this cast with less effort?

class I_BC
{
public:
    virtual ~I_BC() {}
    virtual int getI_BC() = 0;
};

class I_C
{
public:
    virtual ~I_C() {}
    virtual int getI_C() = 0;
};

class A
{
public:
    virtual ~A() {}
    int xx() {return 1;}

    template <typename T>
    T* cast() { return nullptr;}

protected:
    virtual I_BC* cast2BC() {return nullptr;}
    virtual I_C* cast2C() {return nullptr;}
};

template <>
I_BC* A::cast<I_BC>()  {return this->cast2BC();}
template <>
I_C* A::cast<I_C>()  {return this->cast2C();}

class B : public A, public I_BC
{
public:
    int getI_BC() override  { return 0xB000000C;}
    int bar() {return 2;}

protected:
    I_BC* cast2BC() override {return this;}
};

class C : public A, public I_BC, public I_C
{
public:
    int foo() {return 3;}
    int getI_C() override   { return 0xC000000C;}
    int getI_BC() override  { return 0xC00000BC;}

protected:
    I_BC* cast2BC() override {return this;}
    I_C* cast2C() override {return this;}
};


#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    A* a = new B();

    // Ok I know that B implement I_BC interface so cast it now
    I_BC* bc = a->cast<I_BC>();
    cout << "Res : 0x" << hex << bc->getI_BC() << endl;

}
Jonny Schubert
  • 1,393
  • 2
  • 18
  • 39
  • 1
    If virtual methods are working, `dynamic_cast` will too. – Quimby Aug 10 '20 at 14:01
  • 1
    Ok I disabled -rtti but I use virtual methods in my firmware a lot. My knowledge about this topic is based on this web page: https://arobenko.gitbooks.io/bare_metal_cpp/content/compiler_output/rtti.html – Jonny Schubert Aug 10 '20 at 14:07
  • 2
    Herb Sutter's presentation at [CppCon 2019](https://www.youtube.com/watch?v=ARYP83yNAWk) talks (in part) about C++'s guiding _zero-abstraction overhead_ principle and making RTTI "cheaper" by only paying for it if you use it -- and only the parts that are involved. Probably too far out on the horizon to help you out today, but when this capability becomes available you'll be very interested in this development (I'd wager). – Eljay Aug 10 '20 at 14:10
  • @JonnySchubert Oh, I am wrong, you are right, sorry. Virtuals will work without RTTI but dynamic cast won't [link](https://stackoverflow.com/questions/34353751/no-rtti-but-still-virtual-methods). – Quimby Aug 10 '20 at 14:11

1 Answers1

-1

Here’s a custom RTTI implementation that uses macros to reduce boilerplate code: https://www.axelmenzel.de/articles/rtti

Buddy
  • 10,874
  • 5
  • 41
  • 58
  • Thank you for sharing but I can't open the website due to security policy restrictions. – Jonny Schubert Aug 10 '20 at 14:18
  • 1
    I have had weird security at work like that. It can be frustrating when a non malicious site is flagged for some odd reason. With that said link only answers are frowned upon. – drescherjm Aug 10 '20 at 14:24
  • 1
    As it stands, this answer should be a comment. Even if it's helpful and addresses the OP's problem. – cigien Aug 10 '20 at 14:36
  • 2
    Link-only answers are frowned upon on StackOverflow. Links break over time. Please include the relevant code directly in the answer. – Remy Lebeau Aug 10 '20 at 16:28