0

I can't find clear answer about this (apparently) simple case. Assume the following example:

Declaration:

class Foo {

    // blah blah

    Foo& copy(const Foo& other);

    Foo& operator=(const Foo& other);
};

Definition:

inline Foo& Foo::copy(const Foo& other) {
   data_ = other._data;
   return *this;
}

Foo& Foo::operator=(const Foo& other) {
  return copy(other);
}

As you see, I stated the copy() method as inline then I use it in the operator= method. The question is: Does this inline actually have an effect, or the compiler ignore it ?

  • `inline` has always had a "please, compiler" prepended to it. It is a mere suggestion, call it a request to the compiler to consider inlining the function. And compiler is not limited to your suggestions, it can very well inline functions you haven't marked as inline. – Tanveer Badar Jun 07 '18 at 18:02
  • Yes, I know `inline` is only a suggestion, and this is precisly the point of my question. This code compile fine, without error. Logically I guess the compiler should inlines in this case, but I wonder if it there a kind of secret rule with classes which can prevent inline this in this case... – pataproot Jun 07 '18 at 18:22
  • For what it's worth, I'd mark `operator=` as inline, since it does nothing but call another function. That doesn't address whether `copy` will be inlined. – Pete Becker Jun 07 '18 at 18:23
  • My example is very simplistic, one must imagine that the `copy` method is full of complex code, which can be copy-paste to the `operator=` method. Question is : do I have to copy-paste myself, or is the compiler smart enough to do it with the `inline` indication... (I guess the compiler is smart enough, but who know...) – pataproot Jun 07 '18 at 18:30

0 Answers0