Skip to content

Instantly share code, notes, and snippets.

@iboB
Created January 24, 2017 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iboB/dec00b9468db8ea4962e051828a28744 to your computer and use it in GitHub Desktop.
Save iboB/dec00b9468db8ea4962e051828a28744 to your computer and use it in GitHub Desktop.
template <typename T>
struct fast_static
{
// call globally to ensure the constructor is called
static T& slow_get()
{
static T t;
return t;
}
static T* const _fast_var;
// never call globally!
// call after entering main to skip sync of local static
static T& fast_get()
{
return *_fast_var;
}
};
template <typename T>
T* const fast_static<T>::_fast_var = &fast_static<T>::slow_get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment