A
Adam Nielsen
Hi all,
I'm having a go at some template metaprogramming (well sort of) and I'm
stuck trying to figure out if it's possible to pass strings in template
parameter lists.
I am using the code below to convert values in a database into boolean.
(For example, the database stores the strings "ON" and "OFF" but I
want these read in to a variable as boolean true/false.)
The code below works for integers (converting predefined numbers into
true/false) but it doesn't seem to be possible to do the same with
strings. I realise of course I would need a specialisation for the
string comparison (using say strcmp() instead of ==), but for the moment
I'm mainly concerned with whether templates can be used like this or not.
Is this a limitation of C++, or do you just have to code it differently?
Thanks,
Adam.
-----------------------
#include <iostream>
template <typename TDatabase>
class ConvertType {
public:
template <TDatabase T, TDatabase F, bool D>
class ToBool {
public:
static bool toBool(const TDatabase &tTest) {
if (tTest == T) return true;
else if (tTest == F) return false;
else return D;
}
static TDatabase fromBool(bool bTest) {
return bTest ? T : F;
}
};
};
// This works fine
typedef ConvertType<int>::ToBool<0, 1, false> int_to_bool;
// This doesn't
typedef ConvertType<const char *>::ToBool<"ON", "OFF", false>
string_to_bool;
// And neither does this, even thought static const int does
static const char cTrue[] = "ON";
static const char cFalse[] = "OFF";
typedef ConvertType<const char *>::ToBool<cTrue, cFalse, false>
string_to_bool;
// Some example use
int main(void)
{
std::cout << std::boolalpha;
std::cout << "0 == " << int_to_bool::toBool(0) << std::endl;
std::cout << "1 == " << int_to_bool::toBool(1) << std::endl;
std::cout << "ON == " << string_to_bool::toBool("ON") << std::endl;
std::cout << "OFF == " << string_to_bool::toBool("OFF") << std::endl;
return 0;
}
I'm having a go at some template metaprogramming (well sort of) and I'm
stuck trying to figure out if it's possible to pass strings in template
parameter lists.
I am using the code below to convert values in a database into boolean.
(For example, the database stores the strings "ON" and "OFF" but I
want these read in to a variable as boolean true/false.)
The code below works for integers (converting predefined numbers into
true/false) but it doesn't seem to be possible to do the same with
strings. I realise of course I would need a specialisation for the
string comparison (using say strcmp() instead of ==), but for the moment
I'm mainly concerned with whether templates can be used like this or not.
Is this a limitation of C++, or do you just have to code it differently?
Thanks,
Adam.
-----------------------
#include <iostream>
template <typename TDatabase>
class ConvertType {
public:
template <TDatabase T, TDatabase F, bool D>
class ToBool {
public:
static bool toBool(const TDatabase &tTest) {
if (tTest == T) return true;
else if (tTest == F) return false;
else return D;
}
static TDatabase fromBool(bool bTest) {
return bTest ? T : F;
}
};
};
// This works fine
typedef ConvertType<int>::ToBool<0, 1, false> int_to_bool;
// This doesn't
typedef ConvertType<const char *>::ToBool<"ON", "OFF", false>
string_to_bool;
// And neither does this, even thought static const int does
static const char cTrue[] = "ON";
static const char cFalse[] = "OFF";
typedef ConvertType<const char *>::ToBool<cTrue, cFalse, false>
string_to_bool;
// Some example use
int main(void)
{
std::cout << std::boolalpha;
std::cout << "0 == " << int_to_bool::toBool(0) << std::endl;
std::cout << "1 == " << int_to_bool::toBool(1) << std::endl;
std::cout << "ON == " << string_to_bool::toBool("ON") << std::endl;
std::cout << "OFF == " << string_to_bool::toBool("OFF") << std::endl;
return 0;
}