Pete said:
template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) std::swap(l, u);
return c >= l && c <= u;
}
int main()
{
bool c_between_64_122 = isbetween('c', 64, 122);
}
As a bonus, it works for any type where operators >, >=, and <= are
defined.
- Pete
Sorry about all of the confusion, I never compiled it figuring that it was
simple enough that the OP would have no problem fixing it if there were such
a problem. Below is a version that compiles and runs fine under VC7.1 with
extensions off. It did indeed report "ambiguous template parameters" with
the old program.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <algorithm>
using std::swap;
template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) swap(l, u);
return c >= l && c <= u;
}
int main()
{
char c;
cout << "Enter a character: ";
cin >> c;
cout << endl;
if (isbetween<char>(c, 64, 122))
cout << "It is in there" << endl;
else
cout << "It is not in there" << endl;
return 0;
}
BTW, I should warn the OP that checking for ranges in characters like that
is not portable. It will only correctly run on systems that use ASCII.
- Pete