开发者

Best practice for the placement of inline functions in C++

开发者 https://www.devze.com 2023-02-17 21:18 出处:网络
Which is better for short functions (e.g. getters and setters) In the class definition hea开发者_StackOverflow中文版der file

Which is better for short functions (e.g. getters and setters)

  1. In the class definition hea开发者_StackOverflow中文版der file
  2. At the end of the header file
  3. In the source file (in this case should I use the inline keyword or extern inline?)


You can't put inline functions in the source file (and have them used as inline) because their definition won't be available at the points the compiler needs them to inline the code.

Between the other two options, I tend to put one liners into the class definition and any others at the end of the header.


In the class definition header file

typically, unless your build times are more important (assuming that's not the case, based on the question). an exception follows

At the end of the header file

rarely. i have when dealing with nasty templates, just to clean things up. the body is usually so small that it is not distracting, and this can expose implementation details (as a substitute for documentation, but i figure some people will rail me for that). example:

void compute() {
  assert(this->hasEnoughEnergyToCompute());
  ...computing
}

this approach can be good hygiene in some cases. i've actually used secondary files for this (a fourth option).

In the source file

this option is ideal - if it's visible in every translation where you call it. otherwise, send it back to the header.

in this case should I use the inline keyword or extern inline?

just 'inline'. in fact, your compiler probably declares methods inline implicitly. omit it altogether if all your required compilers support this.


My personal preference is to place it within the header file:

class A
{
private:
    int a;

public:
    const int getA() const { return a; }
    void setA(int val) { a = val; }
};

Just because the getX, setX functions are so tiny, they can easily fit in one line.

In general, my get and set methods typically are one line (Only on a few occasions did they require actual non-trivial code).

If you had some non-trivial code in there, like bounds checking or some extra calculations, I would advise against sticking it in the header file.


Where your coding guidelines says to put them. There's no absolute rule, but in general:

-- it's best to avoid inline functions until the profiler tells you you need them, and a lot of programming guidelines forbid them, and

-- it's usually best to avoid as much as possible in the class definition that isn't relevant to the actual interface; most coding guidelines suggest putting the implementation at the end of the header, or even in a separate file included from the header.

Having said that, if the function is so very simple that it fits on the end of the line with the function declaration, I don't think it hurts readability that much.

0

精彩评论

暂无评论...
验证码 换一张
取 消