0

Possible Duplicate:
When should I write the keyword 'inline' for a function/method?

I am not 100% sure, but as far as I know, if I declare a function on an hpp file with the body it will be treated as it is marked as inline(the compiler will decide what to do), I mean:

//myfile.hpp
class StackOverflow{
public:
    void overflow(){_overflow = true;}
...
}

would be the same than:

//myfile.hpp
class StackOverflow{
public:
    ***inline*** void overflow(){_overflow = true;}
...
}

If I am wrong then the question is over, but otherwise, I really like to mark as inline a function even it is not necessary, does general code style guidelines say soemthing about it?

Thanks so much

Community
  • 1
  • 1
Mario Corchero
  • 5,257
  • 5
  • 33
  • 59
  • I know the duplicate isn't exact. But as a question about coding style it wouldn't be a good match for SO – sehe Oct 03 '12 at 09:18
  • 1
    "If I declare a function on an hpp file with the body it will be treated as it is marked as inline". Note that this holds only for member functions. – jrok Oct 03 '12 at 09:20

3 Answers3

4

As the mention of the keyword inline in this context gives no information whatsoever, leave it out. It’s simply visual clutter.

If there were an option to make an inline-defined function non-inline, this would be a different matter (consider private-by-default as such an example): here, it could be argued that even though it’s the default, making the choice explicit makes it easier to understand. But there’s no choice here. No matter what you do, a member function defined inside the body of a class is inline.

In fact, marking it explicitly as inline would be akin to providing other inferred information. You wouldn’t write the following, would you?

#define member
#define function

class StackOverflow{
public:
    member function inline void overflow() { _overflow = true; }
}
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
2

Defining a function body inside the class definition makes the function implicitly same as marking it with keyword inline.

As a matter of coding style, Just defining the function body inside the class is not a good practice.

class Foo {
public:
  void method();  ← best practice: don't put the inline keyword here
  ...
};

inline void Foo::method()  ← best practice: put the inline keyword here
{ ... }

This C++ Faq explains the rationale:

class Fred {
public:
  void f(int i, char c)
    {
      ...
    }
};

Although this is easier on the person who writes the class, it's harder on all the readers since it mixes "what" a class does with "how" it does them. Because of this mixture, we normally prefer to define member functions outside the class body with the inline keyword.
The insight that makes sense of this: in a reuse-oriented world, there will usually be many people who use your class, but there is only one person who builds it (yourself); therefore you should do things that favor the many rather than the few.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

If you declare a function as inline inside of the class, for one just like the register keyword, it's open for the compiler to determine (it's a suggestion) if it's a wise optimization to make. Generally, it's a rule of thumb to use inline functions programmed inline inside of the class itself, however it can be done in the implementation of the class as well, though is not common and the only benefit it offers is the linkage, should you decide to pass out the .h/.hpp file as an API header.

M4rc
  • 473
  • 2
  • 13
  • This is confounding matters. `inline`, first and foremost, indicates **linkage**. This isn’t up for the compiler to decide, it’s mandatory. Unfortunately, the language designers also use it to hint at the optimiser to inline calls to a function declared thusly. – Konrad Rudolph Oct 03 '12 at 09:33
  • Apologies; I should have been a bit more clear on that difference. The way I meant to state it is: the function will be inlined by the compiler if the compiler see's a benefit in doing so. As stated: [link]http://msdn.microsoft.com/en-us/library/z8y1yy88%28v=vs.80%29.aspx[/link] – M4rc Oct 03 '12 at 09:40