-2

usually I wrote only in header or only in cpp, however obviously that's wrong and unusual. Therefore recently I started to get used to separating things into 2 different file.

I'm usually using these keywords:

  • inline
  • __stdcall

My question is the following:

Where should I place them? Only in header? Only in source? Both? Which is the usual convention about it?

My question is ONLY ABOUT THOSE KEYWORDS. It's now about how to generally spearate them. I know already know the basics!

original.roland
  • 640
  • 5
  • 17
  • "usually"? Why would you ever need to specify the calling convention? `inline` is also barely important to modern compilers. – cadaniluk Dec 31 '15 at 11:07
  • Possible duplicate of [Why have header files and .cpp files in C++?](http://stackoverflow.com/questions/333889/why-have-header-files-and-cpp-files-in-c) – Pavel Pája Halbich Dec 31 '15 at 11:16
  • Because I'm working with WINAPIs in a special way. WINAPIs require special calling convention. About the inline, I know my reasons and I just use it. The question is still the usual format. Pavel Pája Halbich: That topic is not even related to my question. I know why they exist, the question is about those 2 special keywords. – original.roland Dec 31 '15 at 11:19

2 Answers2

3

Since an inline function must be inline in all translation units (as you know from the basics), it's simplest to declare it inline in the header. Otherwise you could accidentally forget to define it inline in one source file, while you have defined it inline in another.

In fact, since inline functions must be defined in every translation unit where it's used and the definition must always be the same, it's simplest to also define the inline function in the header and simply include the header in the source files, rather than define it separately in any source file.

When you define the inline function in a header, it's typically recommended to define it out-of-line (might seem paradoxical). This guideline suggested by Tadeusz Kopec is good. You should define the function inline at the definition (in the header, as I suggested), but not at the declaration so that it's easier to later change the definition non-inline if you so later wish. It's also true for non-member functions, but especially true for member functions.

Now, __stdcall is a non-standard keyword, so you'll have to take a look at the documentation of the implementation. The documentation doesn't explicitly state it, but the caller must know the calling convention, and therefore it must be in the header. The documentation explicitly states that the keyword does not need to be in a out of line definition of the function, so you may omit it.

In fact, if you only specify it in the declaration, you don't have to go looking for the definition if you later want to change the calling convention. On the other hand, if the function must be changed when the calling convention is changed, then explicitly defining the calling convention in the definition of the function can serve as a reminder. So, it's a matter of preference.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

I suppose you should place full function signature in the .h file to make sure that person reading your code can understand what you untended without digging into .cpp. Also, quick look at Google C++ style guide didn't help me to find information about placing these 2 words, so it is not a crucial problem.

Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84