1

The documents say:

 A member function that is defined inside its class member list is called an inline member function. 

Then a sample code I wirtten here :

    // main.cpp
    class Screen {
        int width = 0;
        int height = 0;
        
    public:
        int pixels() {
            return width * height;
        }
    };

    int main(int argc, char** argv) {
        Screen s;
        int pixels = s.pixels();

        return 0;
    }

Then, execute 'gcc -S main.cpp' and 'cat main.s', there are codes : asm code

As you see above, there are several independent and complete functions, include 'Screen::pixels()'.

So here, I am puzzled,
why does the document say :
the member functions in the class are inline functions?

In the end, what I am most interested in is :
how to verify it?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Sodino
  • 587
  • 6
  • 16
  • 1
    What did you expect exactly? – spectras Jun 02 '21 at 10:21
  • Inline functions may be inlined, or maybe not. – 김선달 Jun 02 '21 at 10:23
  • 2
    `inline` doesn't mean that function body will be pasted at call site. This is ultimately decided by compiler. `inline` has more to do with linkage and One Definition Rule. – Yksisarvinen Jun 02 '21 at 10:25
  • 2
    the word "inline" is rather overloaded. Here "inline" merely means that the method definition is inside the class definition, not more – 463035818_is_not_an_ai Jun 02 '21 at 10:25
  • @spectras only exist a 'main' function, and ‘pixels()' DOES NOT exist any more. – Sodino Jun 02 '21 at 10:25
  • 1
    Related to [inline-keyword-vs-inlining-concept](https://stackoverflow.com/questions/27042935/inline-keyword-vs-inlining-concept). – Jarod42 Jun 02 '21 at 10:28
  • *"how to verify it?"*. For inlining concept, indeed looking at asm show that. For `inline`, ODR violation are often NDR (No diagnostic required), but linker might spot some duplicate symbols. – Jarod42 Jun 02 '21 at 10:31
  • 1
    @463035818_is_not_a_number No, that is *not* all this means. It also means that the function has internal linkage, as if it were declared `inline`. – Konrad Rudolph Jun 02 '21 at 10:33
  • @김선달 Under what conditions can it be inlined? – Sodino Jun 02 '21 at 10:37
  • @Sodino The answer to that is fairly complex and depends on many factors. You could start by reading the [FAQ](https://isocpp.org/wiki/faq/inline-functions) and the [GCC documentation](https://gcc.gnu.org/onlinedocs/gcc/Inline.html). – Konrad Rudolph Jun 02 '21 at 10:42
  • @KonradRudolph: It’s true that that article “changes its mind” and starts talking about inline functions rather than functions defined in a class, but linkage and inline status are unrelated. – Davis Herring Jun 02 '21 at 13:20
  • @DavisHerring Distinct but definitely not unrelated. I admit simplifying the rules slightly; the actual, relevant rules in the standard span several sections and contain many cross-references. In reality member functions copy the linkage of their containing class. But an inline member function *is an inline function* (§12.2.1/1, §10.1.6/3), and all rules of regular inline functions apply to them. – Konrad Rudolph Jun 02 '21 at 14:11
  • @DavisHerring In case your complaint was solely about my usage of “internal linkage”: yes, that was a mistake, I meant “inline function” — the comment I replied to claimed that *inline member functions* weren’t *inline functions* and the intent of my initial comment was just to correct this. – Konrad Rudolph Jun 02 '21 at 14:18
  • @KonradRudolph: Certainly being defined in a class implies `inline`—except in a C++20 module, which is an exception that may or may not be of interest here. – Davis Herring Jun 02 '21 at 14:51

0 Answers0