Re: static variables in g++
how about this - see below.
It is completely done by the compiler.
No inheritance, no constructors at runtime.
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
using boost::lexical_cast;
template <int FROM, int NO>
struct RangeUp {
static std::string s;
};
template <int FROM, int NO>
std::string RangeUp<FROM, NO>::s (lexical_cast<std::string, int>(FROM)
+ " " + RangeUp<FROM+1, NO-1>::s);
template <int FROM>
struct RangeUp<FROM, 0> {
static std::string s;
};
template <int FROM>
std::string RangeUp<FROM, 0>::s;
template <int FROM, int NO>
struct RangeDown {
static std::string s;
};
template <int FROM, int NO>
std::string RangeDown<FROM, NO>::s (lexical_cast<std::string,
int>(FROM) + " " + RangeDown<FROM-1, NO-1>::s);
template <int FROM>
struct RangeDown<FROM, 0> {
static std::string s;
};
template <int FROM>
std::string RangeDown<FROM, 0>::s;
template <int FROM, int TO>
struct Range {
static std::string up;
static std::string down;
static std::string const& get() { return (FROM < TO) ? up : down; }
};
template<int FROM, int TO>
std::string Range<FROM, TO>::up (RangeUp< (FROM <= TO) ? FROM : 0,
(FROM <= TO) ? (TO - FROM + 1) : 0>::s);
template<int FROM, int TO>
std::string Range<FROM, TO>::down (RangeDown< (FROM >= TO) ? FROM : 0,
(FROM >= TO) ? (FROM - TO + 1) : 0>::s);
int main()
{
std::cout << RangeUp<5,7>::s << std::endl;
std::cout << RangeDown<99,10>::s << std::endl;
std::cout << Range<5,10>::get() << std::endl;
std::cout << Range<17,11>::get() << std::endl;
std::cout << Range<5,5>::get() << std::endl;
return 0;
}
rgds,
Frank
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]