C++: Can virtual inheritance be detected at compile time?

后端 未结 8 424
被撕碎了的回忆
被撕碎了的回忆 2021-02-02 10:55

I would like to determine at compile time if a pointer to Derived can be cast from a pointer to Base without dynamic_cast<>. Is this possible using templates and metaprogramm

相关标签:
8条回答
  • 2021-02-02 11:41

    Once converted to the base pointer, you can only get a runtime error (dynamic_cast). You can define the methods using templated params and get a compile error by using template specializations.

    0 讨论(0)
  • 2021-02-02 11:44

    First, your code is doing sizeof of a pointer instead of a dereferenced pointer so it wouldn't work even if gcc wasn't complaining.

    Second, sizeof trick have to work with casts of 0 and not actual pointers or object - that guarantees zero overhead and also that it won't complie till you do it right.

    3rd, you need to declare 2 templated classes or structs one deriving just from D, the other deriving from D and virtual B, and then cast 0 to their pointers, dereference them and then sizeof.

    4th - Do you have any big reason for trying to be politically correct with static_cast instead of straight cast here? Compiler will always infer from that that you are looking for more whining and in this case you are definitelly not.

    BTW you don't need to grab full code from Alexandrescu - just grab the core technique which basically just:

    sizeof(*((T*)0))
    

    Alexandrescu is really good at cleaning up after a trick.

    Oh and remember that compiler is not supposed to evaluate sizeof args or instantiate unused templated classes and structs - so if it does then it's a compiler bug and if you force it to do that then it's your bug :-)

    Once you have that, you need to define precisely and in positive terms wha your statement "if a pointer to Derived can be cast from a pointer to Base without dynamic_cast<>" actually means in terms of class relationships - just saying "without operator/function Q" doesn't makea problem well defined and you can't solve what you can't define - honest :-)

    So just take the first clean step that compiles and then try to define in which way would the two cases you mentioned be different in reality - what would one have or do that the other one wouldn't.

    0 讨论(0)
  • 2021-02-02 11:44

    If you want to know at compile time you can take the derived class as an parameter but, if the only thing you have is the Base then you cannot know whether it refers to any of the foo,bar,etc.. class. This check can only be done where the pointer is converted to a Base. I think that's the whole purpose of dynamic_cast<>

    0 讨论(0)
  • 2021-02-02 11:47

    Here is a solution for redirecting the compiler to do something depending on whether the class is a subclass of another or not.

    class A 
    {};
    
    class B : virtual public A
    {};
    
    class C : public A
    {};
    
    // Default template which will resolve for 
    // all classes
    template 
    < typename T
    , typename Enable = void 
    >
    struct FooTraits
    {
        static void foo(){
            std::cout << "normal" << std::endl;
        }
    };
    
    // Specialized template which will resolve
    // for all sub classes of A
    template 
    < typename T 
    >
    struct FooTraits 
        < T
        , typename boost::enable_if
             < boost::is_virtual_base_of< A, T>
             >::type
        >
    {
        static void foo(){
            std::cout << "virtual base of A" << std::endl;
        }
    };
    
    int main(int argc, const char * argv[] ){
        FooTraits<C>::foo(); // prints "normal"
        FooTraits<B>::foo(); // prints "virtual base of A"
    }
    

    and if you want to know how boost did it. If you have class Base and class Derived then the following holds.

    struct X : Derived, virtual Base 
    {
       X();
       X(const X&);
       X& operator=(const X&);
       ~X()throw();
    };
    
    struct Y : Derived 
    {
       Y();
       Y(const Y&);
       Y& operator=(const Y&);
       ~Y()throw();
    };
    
    bool is_virtual_base_of = (sizeof(X)==sizeof(Y)));
    

    It's a trick of using virtual inheritence with multiple inheritience. Multiple inheritience from the same virtual base does not result in duplicates of the virtual base class and therefore you can test this with sizeof.

    0 讨论(0)
  • 2021-02-02 11:48

    This may be a little naive (I'm much stronger in C than I am in C++) so I might not understand what you're trying to do, but if it's casting pointers you're talking about, C-style casts work perfectly well (eg. (D *)foo), or the C++ equivalent reinterpret_cast. That being said, this can be very dangerous, because you don't have any runtime checking, and thus need to be sure that you're casting into the correct type. Then again, if you wanted to have an easy way to check whether this is a correct assumption or not, we are back to square one. However, it appears you are trying to compare pointers above, which are all the same (they are basically integers). As far as I know, there is no way to determine an object's class at runtime in C++, including sizeof, which works at compile time. Basically, there's no way to do what you want to do (at least not with standard C++), however the actual cast wont cause any issues, just using the newly cast pointer in improper ways. If you absolutely need this functionality, you would probably be best to include a virtual function in your base class that reports what class it is (preferably with an enum value), and overload it in whichever subclass you hope to determine whether you can cast too.

    0 讨论(0)
  • 2021-02-02 11:50

    Have you tried SUPERSUBCLASS from Loki?

    http://loki-lib.sourceforge.net/

    0 讨论(0)
提交回复
热议问题