0

I was wondering if there is a sort of naming convention when using inline functions.

Say I am implementing an API and want to let the user decide if he wants to use a normal function or an inline function(both doing the same thing), is there a naming convention how to make it visible to the outside world that a function is potentially inline ? I know that I could look into the .h or .inl file

EDIT: I feel the need to clarify some points since it seems that I wasn't clear enough in the first place.

Lets say I have a function which accepts three values. Something like:

bool MyClass::AddTwoNumbersAndCheckIfBigger(int summandOne, int summandTwo, int biggerNr)
{
    bool isBigger = false;
    int sum = summandOne+summandTwo;
    if(sum > biggerNr)
    {
        isBigger = true;
    }
    return isBigger;
}

This function could be called a single time up to x > 1000000. If I call it once I would prefer it to just be called normally but if it is called 1000000 times in a for loop I maybe prefer an inline option.

I want to give the developer the possibility to choose between those two options. I am aware of the fact that the compiler is responsible for deciding if its going to be used as an inline function or not but I guess it would be nice if the developer could decide for himself.

The question for the naming convention arises from the fact that I have users\developers who don't look at the header files but only use intellisense for guidance.

Bongo
  • 2,933
  • 5
  • 36
  • 67
  • 2
    Preface the name with `inline_`? There is no "standard" I know of for this. – owacoder Oct 20 '15 at 12:35
  • 4
    Why would you want to force the user to choose between non-inline and inline? – Emil Laine Oct 20 '15 at 12:38
  • Why not just use inline keyword..? (http://en.cppreference.com/w/cpp/language/inline) – badola Oct 20 '15 at 12:48
  • 1
    @zenith: "force" is a loaded word; e.g. they may *want* to pick an inline version when they know performance is critical, but prefer an out-of-line version when they want to pick up updated implementations from newer versions of a dynamic library. (It's very much the exception that clients are left this freedom though - normally the library author wants to control how updates are received, and ensure all implementation remains consistent.) – Tony Delroy Oct 20 '15 at 13:21
  • @owacoder: I believe that's an answer... best posted as such. – Tony Delroy Oct 20 '15 at 13:25
  • 1
    You are creating an API which is hard to use correctly and easy to use incorrectly. It should be the opposite. By the way, *"I have users\developers who don't look at the header files but only use intellisense for guidance*" - Perfect, that's exactly how it should be. Why should I look at the header files instead of reading the documentation of the class? – Christian Hackl Oct 20 '15 at 14:15
  • @ChristianHackl the part with the header was in reply to an answer I received. How would you give the user the freedom of choice which option he wants to use ? – Bongo Oct 20 '15 at 14:19
  • 1
    @Bongo: As far as API design is required, freedom *from* choice is as important as freedom *of* choice. As an API client, I probably do not want to invest time into examining my choices and finding out if you guarantee identical results or not. You are also promising something which you cannot deliver, because whether something is inlined or not is largely out of your control. It seems that you trying to do premature optimisation on the API design level! – Christian Hackl Oct 20 '15 at 14:26
  • 1
    "I want to give the developer the possibility to choose between those two options" — I believe the compiler should be the one to have that control. From what I've heard, the compiler is much better at deciding what to inline and what not to inline. Furthermore, as pointed out by Christian Hackl, who would want to waste their time deciding between two functions of identical functionality, especially when 99% of the time such micro-optimizations won't even matter. I have not yet seen a library that would provide 2 versions of their API functions, and I think there's a reason for that. – Emil Laine Oct 20 '15 at 14:58
  • 1
    @Bongo you can use this naming convention but as many answers and comments indicated providing such differentiation is not recommendable. Please, note that providing such naming may cause confusion. Some developer could claim that he didn't experiment any improvement even when he is using the "inline" version of your API. So, that could perfectly happen if the compiler decides to NOT inline the function and you can't force it to inline the function call. – rkachach Oct 20 '15 at 15:21
  • @redobot I won't use this naming convention. As I stated in a comment below the answer that I consider it the right answer because the answer and the comment said that there is no naming convention. This answers my question. – Bongo Oct 20 '15 at 15:38

4 Answers4

6

If the library for which you are exposing an API is compiled, then inline is ineffective.

If you're supplying an API with explicit inline functions, then that will be obvious to the user since the function body will have to be in the header.

So no naming convention is necessary as it's always obvious at the point of use.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
3

I'll not recommend adding any prefix to the "inlined" functions. Because of several reasons:

  1. Does it matter for a user of this function if it's inline or not?
  2. The "inline" keyword is only a hint to the compiler, so at the end of the day it's up to the compiler to treat the function as such or not
  3. Functions declared in the header (in the class definition) are already "inline" so adding the keyword is redundant and not necessary.
rkachach
  • 16,517
  • 6
  • 42
  • 66
  • 1
    *"Functions declared in the header are already "inline" "* - not implicitly (the compiler proper's pretty much oblivious to whether content is in a header or not - it operates in terms of translation units). A vaguely similar thing you might be thinking of is functions in a class definition - they're implicitly inline. Or, you could argue that anyone putting a function definition in a header's likely to mark it explicit inline, otherwise they risk running afoul of the One Definition Rule.... – Tony Delroy Oct 20 '15 at 13:07
  • Yes, I wanted to say in the class definition. I edited my answer to reflect this fact. – rkachach Oct 20 '15 at 13:17
1

If your inline functions are in a header file to be included by the end user, you could distinguish between the two by prefacing the names of the inline functions with inline_ or something similar:

inline void inline_my_func(void) {...} //inlined function (may or may not be inlined by compiler)
void my_func(void); //non-inlined function, same operation, defined elsewhere

However, this would only hint to the end user that the function might be inlined, not that it actually is, as the other answers have pointed out. The name would only point out to a user that never looked at the header that the function was declared inline.

I don't know of any naming convention for inlined functions, and probably, none exists. This is partially because one cannot guarantee that a particular function will be inlined. A compiler is free to take the inline keyword as a hint, and disregard it.

owacoder
  • 4,815
  • 20
  • 47
  • Your comment and this answer provided an answer to the question I was asking for, which was: "If there is a naming convention for inline functions". The answer is no, so I consider this the right answer. – Bongo Oct 20 '15 at 15:13
0

inline function may be simultaneously not inline without any prefixing. It will be inlined in code in normal position, but if you'll take it's address, compiler must generate a code for it. Thus, I think, using pointers to a function and calling by a pointer is a solution. If you create a library and use inline functions internally, you can do some static object that creates pointers of inline functions, and their code will be generated in the library. If you have a code in the header itself, the user can call the body of function, taking the address himself whenever he wants, or you can do a pointer for him and export in the header the both: definition of inlined function and pointer to the generated body. The pointer can have any name, you want, and be used approximately as a usual function. Just document it. C++11:

#include <iostream>
inline int fun(int) { return 3; }

auto pfun = &fun;

int main()
{
  std::cout << pfun(4) << std::endl;
  return 0;
}
Роман Коптев
  • 1,555
  • 1
  • 13
  • 35