virtual inline

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • .rhavin grobert

    virtual inline

    if you have

    class Base {
    virtual inline bool foo() {return false;};
    };

    class Derived: public Base {
    virtual bool foo();
    };


    bool Derived:foo()
    {
    /* lot's of code here */
    return true;
    };

    is foo() always inlined for Base? is foo always (jumped /
    called / ...) (<-?) for Derived?
  • Victor Bazarov

    #2
    Re: virtual inline

    ..rhavin grobert wrote:
    if you have
    >
    class Base {
    virtual inline bool foo() {return false;};
    };
    >
    class Derived: public Base {
    virtual bool foo();
    };
    >
    >
    bool Derived:foo()
    {
    /* lot's of code here */
    return true;
    };
    >
    is foo() always inlined for Base?
    What does that mean?
    is foo always (jumped /
    called / ...) (<-?) for Derived?
    What does that mean?

    A function cannot be "always inlined" or "never inlined". You have no
    control over that - the compiler is free to do what it thinks is best,
    neither do you have any way of knowing what the compiler did - there is
    no portable way to determine if a particular function was ever inlined
    or not. Why do you care?

    The concepts of dynamic binding (virtual functions) and inlining are
    orthogonal. If the compiler knows that it needs to call 'Base::foo' and
    it has the implementation handy (and it's short like that), it will
    *probably* (or, perhaps, *hopefully*) inline it. If the compiler needs
    to involve run-time resolution (i.e. it has a pointer or a reference to
    'Base' and calls 'foo' with that), it will generate code to call the
    function in a particular way (v-table, etc.) and "inlining" has nothing
    to do with that.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • Pete Becker

      #3
      Re: virtual inline

      On 2008-11-14 12:50:25 -0500, ".rhavin grobert" <clqrq@yahoo.de said:
      if you have
      >
      class Base {
      virtual inline bool foo() {return false;};
      };
      >
      class Derived: public Base {
      virtual bool foo();
      };
      >
      >
      bool Derived:foo()
      {
      /* lot's of code here */
      return true;
      };
      >
      is foo() always inlined for Base? is foo always (jumped /
      called / ...) (<-?) for Derived?
      Ask your compiler. There's no requirement that inline functions be
      expaned inline.

      --
      Pete
      Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
      Standard C++ Library Extensions: a Tutorial and Reference
      (www.petebecker.com/tr1book)

      Comment

      • =?ISO-8859-1?Q?Marcel_M=FCller?=

        #4
        Re: virtual inline

        Hi!

        ..rhavin grobert schrieb:
        class Base {
        virtual inline bool foo() {return false;};
        };
        >
        class Derived: public Base {
        virtual bool foo();
        };
        >
        bool Derived:foo()
        {
        /* lot's of code here */
        return true;
        };
        >
        is foo() always inlined for Base? is foo always (jumped /
        called / ...) (<-?) for Derived?
        From your code Base::foo is never called at all, so you cannot answer
        the question whether it is expanded inline or not.

        Derived::foo cannot be expanded inline unless your compiler uses a two
        pass method to examine the body of the function.

        Furthermore, a virtual function may not be expanded inline unless the
        compiler knows for sure, that it cannot be overloaded. The reason is
        simply that the required function is determined at runtime and may not
        even be written at the time when the compiler generates the call.
        Only if the type is known for sure, the run time dispatch can be
        optimized. E.g.
        Base b;
        b.foo();
        may expand Base::foo inline.
        But as soon as b becomes a reference or pointer type, a run time
        dispatch is required.
        (Languages with a 'final' attribute for functions and classes have a
        significantly higher probability of the above optimization.)


        Marcel

        Comment

        • Pete Becker

          #5
          Re: virtual inline

          On 2008-11-15 04:49:13 -0500, Marcel Müller
          <news.5.maazl@s pamgourmet.coms aid:
          >
          Furthermore, a virtual function may not be expanded inline unless the
          compiler knows for sure, that it cannot be overloaded.
          Overloading has nothing to do with it.
          The reason is simply that the required function is determined at
          runtime and may not even be written at the time when the compiler
          generates the call.
          This is not overloading. It is overriding.
          Only if the type is known for sure, the run time dispatch can be
          optimized. E.g.
          Base b;
          b.foo();
          may expand Base::foo inline.
          But as soon as b becomes a reference or pointer type, a run time
          dispatch is required.
          No, not required. Just quite common. But if the compiler can determine
          the actual type of the object it can expand the function inline.

          --
          Pete
          Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
          Standard C++ Library Extensions: a Tutorial and Reference
          (www.petebecker.com/tr1book)

          Comment

          • mail.dsp@gmail.com

            #6
            Re: virtual inline

            On Nov 14, 10:50 pm, ".rhavin grobert" <cl...@yahoo.de wrote:
            if you have
            >
            class Base {
            virtual inline bool foo() {return false;};
            >
            };
            >
            class Derived: public Base {
            virtual bool foo();
            >
            };
            >
            bool Derived:foo()
            {
            /* lot's of code here */
            return true;
            >
            };
            >
            is foo() always inlined for Base? is foo always (jumped /
            called / ...) (<-?) for Derived?
            For more information visit this link:


            --
            Daya

            Comment

            Working...