jeudi 23 mai 2019

Will the virual call to function be inlined when it should be able to?

Sorry, if there are errors in code. Currently writing from mobile.

Consider following scenario:

 class A
 {
       public:
       int funX()
       {
              return funY()*5;
       }
       virtual int funY() = 0;
 };

 class B 
        public: A
 {
  public:
       int funY() override final
       {
            // implementation
       }
 };

 int foo(B& b)
 {
       return b.FunX();
  };

In this code compiler has enough information to determine that it needs to call B::funY() without virtually calling it. However, it wouldn't be the case without the final keyword. Else one could send an instance of a possible class C that implements funY() differently.

Do the compliers optimize it away or not?

Aucun commentaire:

Enregistrer un commentaire