The top-rated answer above does a pretty good job addressing the question. But I was quite curious about the last paragraph:
In a nutshell, if you use static, then taking the address of the function in different translation units will return different addresses (because you're telling the compiler to generate a function in each TU), but if you use inline, they'll show the same address (because you're defining one function, and just telling the compiler to merge the many definitions together).
So, I wrote small programs for better understanding:
funcs.hpp:
#pragma once
#include <iostream>
static void staticFunc() {
std::cout << "inside staticFunc" << std::endl;
}
inline void inlineFunc() {
std::cout << "inside inlineFunc" << std::endl;
}
translation_unit_1.cpp:
#include "funcs.hpp"
void printFuncAddressesTu1() {
std::cout << "Translation Unit 1 - Address of staticFunc: " << (void*)staticFunc << std::endl;
std::cout << "Translation Unit 1 - Address of inlineFunc: " << (void*)inlineFunc << std::endl;
}
translation_unit_2.cpp:
#include "funcs.hpp"
void printFuncAddressesTu2() {
std::cout << "Translation Unit 2 - Address of staticFunc: " << (void*)staticFunc << std::endl;
std::cout << "Translation Unit 2 - Address of inlineFunc: " << (void*)inlineFunc << std::endl;
}
main.cpp:
#include <iostream>
// Declrations of functions in different translation units
void printFuncAddressesTu1();
void printFuncAddressesTu2();
int main() {
printFuncAddressesTu1();
printFuncAddressesTu2();
return 0;
}
Upon running g++ main.cpp translation_unit_1.cpp translation_unit_2.cpp && ./a.out, it prints:
Translation Unit 1 - Address of staticFunc: 0x5586be0f9218
Translation Unit 1 - Address of inlineFunc: 0x5586be0f931a
Translation Unit 2 - Address of staticFunc: 0x5586be0f9349
Translation Unit 2 - Address of inlineFunc: 0x5586be0f931a
As seen in the above output, inlineFunc calls have the same address but the staticFunc calls have different addresses!
Hope this helps!
static inlineused together. Just the semantic differences between the two as it applies to my contrived example. Unfortunately the answers there did not satisfy the unknowns for me.