Is it always true that a virtual function cannot be embedded in C ++?

It is said that virtual functions cannot be built-in. Is it always true that if a function is declared virtual, it cannot be bound anywhere in the code or is it used only in certain situations? (e.g. call method from base pointer versus call method by reference, etc.)

+7
source share
6 answers

No, virtual functions really can be built in. Virtual dispatching is used only with a polymorphic call to the virtual method (i.e., on a pointer or object reference). However, when the virtual method is called on the value of the object, virtual dispatch is not used, and the compiler is free from the built-in at its discretion.

+8
source

Given:

struct T { virtual void foo() { /* something */ } }; 

Using polymorphism (If you call foo() via pointer-to-T or reference-to-T )

 T* ptr = get_ptr_somehow(); ptr->foo(); 

If the compiler knows that T is the only node in the inheritance tree, it can refuse virtual calls and potentially enable the function. However, this is an incredibly unlikely scenario, and I doubt it can even be detected. In all other cases, the attachment is impractical due to the sending of the execution time.

Static dispatch (if you call foo() on a standard object)

 T obj; obj.foo(); 

In this case, the compiler knows that the dynamic type of obj is T , that it does not require a virtual send, and therefore can embed the function code if it wants to.

 T* ptr = get_ptr_somehow(); ptr->T::foo(); 

In this case, the compiler does not know the dynamic type of obj , but it knows which function it will call, knows that it does not require a virtual send, and therefore can embed the function code if it wants to.

+6
source

A virtual member function is basically a pointer to a function that points to the correct function to call depending on the implementation of the class hierarchy. (note that it does not have to be a pointer, but it is often implemented like this because it seems to be the most efficient implementation for this abstraction).

This means that knowing which function will be called is determined only at runtime.

So, how could you embed a function that is known only at runtime? Impossible.

If you do not have a JIT compiler :) - the_drow

True Not common, and you may lose some performance on boot, but still true.

+3
source

Objects declared on the stack or inside classes, for example, do not require dynamic dispatch to their virtual functions, which are called because the compiler knows the types of all objects on the stack for facts, so basically any value. Sending a virtual function occurs only through pointers or links.

+2
source

With optimizations, very little is always true. In general, if the compiler cannot determine what the actual type of the object is, virtual functions should be enabled at run time. If you have an actual instance (Class versus Class &) then the compiler can be sure. If you have a link, you have to guess; if the calling function itself is built-in, it may identify the static type in this way, or it may not.

Please note that in this case there is no difference between the pointer and the link. In any case, you get polymorphism at runtime.

+1
source

Virtual functions (methods) can be embedded. Remember that the inline is a suggestion to the compiler, not a requirement.

The attachment code is similar to inserting a function as it is, where necessary in an executable file. A separate function is not created.

When there are pointers to an inline function (internally using the compiler or externally by the programmer), the insert may not be used. There must be one instance of an inline function when there are pointers to it. The compiler is free to insert code, but there must be one instance in a static (fixed) place to satisfy the pointers.

My suggestion is simple, less than 5 lines of code, without using a profiler. Remember that inlining usually puts code in a header file. When the method code is changed, all translation units depending on the header file must be rebuilt. This can be expensive in large systems. As a rule, large pieces of code should only be embedded after , they are checked and only to increase efficiency, as dictated by the profiler.

0
source

All Articles