Надругательство над С++. Пожалуйста, не бейте меня.
От: _Winnie Россия C++.freerun
Дата: 21.11.04 12:52
Оценка: 75 (11) :))) :))) :))

#include <map>
#include <sstream>
#include <string>

using std::string;

struct VarValue
{
  std::string str;

  VarValue() {}

  template <class T> operator T() 
  { 
    T x;
    std::istringstream stream(str);
    stream >> x;
    return x;
  }

  template <class T>  void Assign(T x)
  {
    std::ostringstream stream;
    stream << x;
    str = stream.str();
  }

  template <class T> VarValue(T x)
  {
    Assign(x);
  }

  template <class T> VarValue &operator=(T x)
  {
    Assign(x);
    return *this;
  }

  //для большей путаницы можно другие операторы определить.
};

std::map<std::string, VarValue> variables_;
#define $(var) (variables_[#var])

#include <iostream>








//использование

int main()
{
  $(hello) = "test";
  $(world) = 1;
  $(other_var) = int($(world)) + 10;

  std::cout << (string)$(hello) + (string)$(other_var);
}
Правильно работающая программа — просто частный случай Undefined Behavior
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.