Re: [RESOLVED] Variable name
Here's a bit more generic solution:
Code:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
struct LessThanException
{
LessThanException(int int_, int threshold)
{
ostringstream str;
str << "IntLessThan" << threshold << ": bad value: " << int_;
msg = str.str();
}
string msg;
};
template <int t_threshold>
class IntLessThan
{
int m_int;
public:
operator int()
{
return m_int;
}
IntLessThan(int int_): m_int(int_)
{
if (m_int >= t_threshold)
throw LessThanException(m_int, t_threshold);
}
};
typedef IntLessThan<10> IntLessThan10;
void foo(int a)
{
IntLessThan10 dummy(a); // just check
cout << __FUNCTION__ << ": Succeeded with " << a << endl;
}
template <int t_threshold>
void Foo(int a)
{
IntLessThan<t_threshold> dummy(a); // just check
cout << __FUNCTION__ << ": Succeeded with " << a << endl;
}
int main()
{
try
{
foo(-12); // threshold prefefined by foo implementation
foo(7);
Foo<20>(17); // threshold explicitly specified
Foo<15>(17);
}
catch(LessThanException e)
{
cout << e.msg << endl;
}
return 0;
}
Code:
D:\Temp\23>23.exe
foo: Succeeded with -12
foo: Succeeded with 7
Foo: Succeeded with 17
IntLessThan15: bad value: 17