1

Are there cases in which the inline keyword on a virtual method is not ignored by the compiler?

I am thinking for example to cases such as the following:

struct Interface
{
    virtual void f() = 0;
};

struct Dummy : Interface
{
    inline virtual void f() override {}
};

int main()
{
    Dummy a;
    a.f();

    return 0;  
}

Can code like this be optimized?

nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
  • 3
    http://stackoverflow.com/q/27042935/1171191 – BoBTFish Dec 16 '15 at 10:08
  • 8
    Member functions that are defined inline are implicitly `inline` anyway. – Kerrek SB Dec 16 '15 at 10:10
  • 1
    confusingly, the inline `keyword` is not a hint to the compiler that code should be inlined. This is a common misconception. It merely tells the compiler that the definition of the function's body may appear more than once (provided that each copy is are exactly the same). inline is implicit when member functions are defined within a class declaration AND when expanding template functions (of any kind) and members of template classes. – Richard Hodges Dec 16 '15 at 11:13
  • @RichardHodges to be pedantic, the standard disagrees with your first statement as well. `[dcl.fct.spec]/2 ... The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism.` But of course, it's indeed not correct to think that it's only a hint because what you said about inline function being allowed to be defined multiple times (in separate translation units) is true. – eerorika Dec 16 '15 at 11:36
  • @user2079303 pedantry in computer sciences is a virtue. I stand corrected. :) – Richard Hodges Dec 16 '15 at 11:45

2 Answers2

3

Read the wikipage on virtual method table. Some optimizing compilers may do some devirtualization (recent GCC has some optimization passes doing that).

BTW, compile your example, using GCC 5.3 (on Linux/Debian/Sid/x86_64), with g++ -std=c++11 -Wall -fverbose-asm -S -O2; you see that main is compiled into:

    .type   main, @function
 main:
.LFB1:
    .file 1 "ex.cc"
    .loc 1 12 0
    .cfi_startproc
 .LVL0:
    .loc 1 17 0
    xorl    %eax, %eax      #
    ret
    .cfi_endproc

so your program is optimized into the equivalent of int main() {return 0;} (BTW, adding an int foo; field in Interface and a constructor & destructor setting it does not change the resulting optimized main)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Are there cases in which the inline keyword on a virtual method is not ignored by the compiler?

inline keyword is never ignored. However, it's always up to the optimizer to decide whether a function is expanded inline or called. Even virtual function calls can be expanded inline if the run time type is known at compile time.

In this case, the keyword is redundant since member functions that are defined inside the class definition would be implicitly inline anyway.

Can code like this be optimized?

It can, because the optimizer can deduce the runtime type of the object in this context.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 2
    afaik the `inline` keyword is just a hint for the compiler and can be ignored – 463035818_is_not_an_ai Dec 16 '15 at 10:34
  • https://isocpp.org/wiki/faq/inline-functions – Sorin Dec 16 '15 at 10:35
  • 1
    @tobi303 it is indeed a hint to the compiler that inline expansion is preferred to a function call and the hint can indeed be ignored. But inline is not *just* a hint. *An inline function shall be defined in every translation unit in which it is odr-used*. Unlike non-inline functions, which shall not be defined in more than one translation unit. That rule cannot be ignored by the compiler. – eerorika Dec 16 '15 at 11:22