Thread: Function Inlining

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    104

    Function Inlining

    My school forbids function definitions and .c file inclusions in header files.
    Is there a way to inline a function that is declared in a header file and defined in a .c file, while following the above mentioned constraints?

    At school we use macs, which apparently, have some strange mix of gcc 4.2 and llvm 9.1.

    Code:
     gcc --version
    Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
    Apple LLVM version 9.1.0 (clang-902.0.39.1)
    Target: x86_64-apple-darwin17.5.0
    Thread model: posix
    InstalledDir: /Library/Developer/CommandLineTools/usr/bin
    Thank you.
    Last edited by Dren; 04-08-2018 at 01:29 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,731
    Without functions, all you can use are preprocessor macros. Why does your school forbid the use of functions though?
    Devoted my life to programming...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,930
    Quote Originally Posted by Dren
    My school forbids function definitions and .c file inclusions in header files.
    Is there a way to inline a function that is declared in a header file and defined in a .c file, while following the above mentioned constraints?
    The reason why you would declare a function in a header file is so that the header file could be included in multiple source files, and hence the function could be conveniently called in multiple translation units. For a function to be inline, it has to be defined in the same translation unit as it is declared, which implies that it has to be defined in the same translation unit as it is called, since it should be declared before being called.

    Therefore, the answer to your question is yes: you could declare it as inline in the header, and then define it in each source file where it is used, but that is an absurd solution (and I think you'll run into redefinition errors unless you also declared it static so as to give it internal linkage).

    Can you demonstrate that with inlining, your code will experience a speedup that is both significant and important to what you are trying to do? If so, approach your instructors to get the rule changed because you have demonstrated that it is a silly rule insofar as it does not consider function inlining. If not, then there's no real benefit inlining, so just write the code as prescribed and move on.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    104

    Post

    Quote Originally Posted by laserlight View Post
    Therefore, the answer to your question is yes: you could declare it as inline in the header, and then define it in each source file where it is used, but that is an absurd solution.
    I agree. But at least I can do it in those cases where it is beneficial and necessary, performance wise; I will *have* to declare it static as well, as that is another rule at my school.

    And thanks for explaining header-source file interaction in a clear and illustrative manner.
    Quote Originally Posted by laserlight View Post
    Can you demonstrate that with inlining, your code will experience a speedup that is both significant and important to what you are trying to do?
    I am writing a library, so technically, not for this project. But I have other projects that would easily prove my case. Not that it would matter anyhow, as the rules here are unchangeable. We can't use for loops, switch-case statements, or interlinked ternary operators either. Nor can we have more than 5 function definitions per source file...

    Go figure

  5. #5
    Registered User
    Join Date
    May 2016
    Posts
    104
    Quote Originally Posted by GReaper View Post
    Without functions, all you can use are preprocessor macros. Why does your school forbid the use of functions though?
    The use of function is not forbidden, function definitions in a header file is. As long as I define them in a source file, all is good.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,930
    Quote Originally Posted by Dren
    I will *have* to declare it static as well, as that is another rule at my school.
    What do you mean? In general, you should only declare a function static if it is meant to be implementation detail, since it would then have internal linkage, hence avoiding the problem of a name collision with another implementation detail function in a different translation unit. As such, how can that possibly be a rule at your school? Surely you don't mean that the rule requires you to declare all functions static, even those declared in header files so as to be included in multiple translation units?

    Quote Originally Posted by Dren
    We can't use for loops, switch-case statements, or interlinked ternary operators either. Nor can we have more than 5 function definitions per source file...
    Well, there are good reasons why you might not want to nest ternary operators (i.e., readability and it is quite plausible readers will get the precedence mixed up if you don't enforce grouping with parentheses), but the others are unusual unless it is for a beginner's exercise.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2016
    Posts
    104
    Quote Originally Posted by laserlight View Post
    Surely you don't mean that the rule requires you to declare all functions static, even those declared in header files so as to be included in multiple translation units?
    Lol, no, no. Only functions with local scope. I was thinking I will have to declare whatever function I chose to inline, as static, because it will have local scope only; I won't declare it in a header.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    3,883
    Quote Originally Posted by Dren View Post
    My school forbids function definitions and .c file inclusions in header files.
    Is there a way to inline a function that is declared in a header file and defined in a .c file, while following the above mentioned constraints?

    At school we use macs, which apparently, have some strange mix of gcc 4.2 and llvm 9.1.


    Thank you.
    NOTE: I read the above as your school allows function prototypes in header files, is this correct?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    3,883
    Links Inline function - Wikipedia
    and Inline Functions In C

    Showed me something new the use of "static inline".

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    My school forbids function definitions and .c file inclusions in header files.
    Okay, this is a good rule. You should only have macros and function declarations (prototypes) in header files. Source files (.c) should never be included in any file, you add the file to your project/build line to be compiled. Header files should not contain executable code (with rare exceptions).

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,930
    Quote Originally Posted by jimblumberg
    You should only have macros and function declarations (prototypes) in header files.
    If it is indeed meant to be used in multiple translation units, an inline function with external linkage should be defined in a header file. If not, then perhaps it should have internal linkage instead and be defined in a source file. EDIT: come to think of it, if you want to define the multiply-used inline function externally, I guess that could still work, but I suspect that you'll then depend on the compiler doing whole program optimisation in order for inlining to actually happen even for functions that are good candidates for inlining.
    Last edited by laserlight; 04-08-2018 at 04:24 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    an inline function with external linkage
    I doubt that inline functions are allowed since function definitions are not allowed in header files:
    My school forbids function definitions and .c file inclusions in header files.
    IMO, inline functions are rarely (if ever) needed in beginning projects and this project is probably meant to teach the use of headers and multiple source files.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,930
    Quote Originally Posted by jimblumberg
    I doubt that inline functions are allowed since function definitions are not allowed in header files
    They didn't forbid it, and hence Dren's plan of defining inline functions with internal linkage within source files could still be within the rules.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inlining functors
    By cmajor28 in forum C++ Programming
    Replies: 20
    Last Post: 03-30-2015, 10:11 PM
  2. Compiler Inlining Question
    By golfinguy4 in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2012, 02:46 AM
  3. inlining functions
    By C_ntua in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2009, 10:45 PM
  4. gcc and inlining
    By Laserve in forum C Programming
    Replies: 4
    Last Post: 11-24-2006, 03:31 AM
  5. What is inlining?
    By codegeek in forum C++ Programming
    Replies: 12
    Last Post: 02-04-2005, 02:25 PM

Tags for this Thread