Skip to content

Instantly share code, notes, and snippets.

@2bbb
Created February 21, 2018 13:51
Show Gist options
  • Save 2bbb/63b37897734d92e414422bcf7965cb5e to your computer and use it in GitHub Desktop.
Save 2bbb/63b37897734d92e414422bcf7965cb5e to your computer and use it in GitHub Desktop.
inline_variable
#include <iostream>
template <typename type, typename tag = type>
struct inline_variable {
static type &get(const type &initial_value) {
static type _{initial_value};
return _;
}
};
struct p_tag;
static int &p{inline_variable<int, p_tag>::get(4)};
int main(int argc, char *argv[]) {
std::cout << p << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment