#include <iostream>
#include <map>
#include <string>


#define cacheVar(type, namevar) varCache<type, namevar>()

char var1[]="var1";
char var2[]="var2";



template<typename T>
std::map<std::string, T>& getGlobalCache()
{
  static std::map<std::string, double> theDoubleCache;
  return theDoubleCache;
}


template< typename T >
T* findVar( const std::string &name )
{
  return &getGlobalCache<T>()[ name ];
}


template< typename T, const char* name>
T& varCache()
{
  static T* _cache = 0;
  if ( _cache == 0 )
    {
      //const char* name = static_cast<const char*>(vname);
      _cache = findVar<T>(name);
    }

  return *_cache;
}




int main()
{
  cacheVar( double, var1 ) = 3;
  cacheVar( double, var2 ) = 4;

  std::cout << varCache<double, var1>() << std::endl;
  std::cout << varCache<double, var2>() << std::endl;

  cacheVar(double, var1) = 13;

  std::cout << cacheVar(double,var1) << std::endl;
  std::cout << cacheVar(double,var2) << std::endl;


}

JörgenSamson/TemplateFun (last edited 2010-06-03 10:10:11 by JörgenSamson)