C++ On Demand Global Variable

template<int label>
int& gInt()
{
  static int n = int();
  return n;
}
Usage
#define DRINKING(id) id*3 + 0
#define EATING(id) id*3 + 1
#define COUNT(id) id*3 + 2

template<int id>
void logDiet(int d, int e);
{
  gInt<DRINKING(id)>() += d;
  gInt<EATING(id)>() += e;
  gInt<COUNT(id)>()++;
}

enum class Diet{
  Breakfast,
  Lunch,
  Dinner
};

logDiet<Diet::Breakfast>(200, 500);
logDiet<Diet::Lunch>(150, 700);
comments powered by Disqus