Skip to content

Instantly share code, notes, and snippets.

@klmr
Created June 22, 2016 09:34
Show Gist options
  • Save klmr/9e06284a1ca9152bee2bfc3b1df14003 to your computer and use it in GitHub Desktop.
Save klmr/9e06284a1ca9152bee2bfc3b1df14003 to your computer and use it in GitHub Desktop.
Difference between static and inline functions in C++
inline int f() {
static int x = 0;
return ++x;
}
static int g() {
static int x = 0;
return ++x;
}
#include "header.hpp"
#include <iostream>
void test();
int main() {
std::cout << "From main:\n";
std::cout << "f() = " << f() << ", g() = " << g() << '\n';
std::cout << "From test:\n";
test();
}
#include "header.hpp"
#include <iostream>
void test() {
std::cout << "f() = " << f() << ", g() = " << g() << '\n';
}
@klmr
Copy link
Author

klmr commented Jun 22, 2016

Output:

From main:
f() = 1, g() = 1
From test:
f() = 2, g() = 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment