/*
*
* The output of this with gcc 4.7.2 is:
*
* 1
* 2
* this generic-int function should not be called
* 0
*
*
*
* The output of this with gcc 4.6.3 is:
*
* 1
* 2
* this type-bool function SHOULD BE called
* 1
*
*/
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstring>
struct Type { int dummy; };
template <typename T, typename M>
inline T extractTag(const M &map, const char *key)
{
if (!strcmp(key, "blah-bool"))
std::cout << "this generic-int function should not be called"
<< std::endl;
T ret = 0;
std::istringstream stream(extractTag<std::string>(map, key));
stream >> ret;
return ret;
}
template <typename M>
inline bool extractTag(const M &map, const char *key)
{
if (!strcmp(key, "blah-bool"))
std::cout << "this generic-bool function should not be called"
<< std::endl;
std::string str(extractTag<std::string>(map, key));
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
return (str == "1" || str == "true");
}
template <>
inline unsigned int extractTag(const Type&, const char *key)
{
return 2;
}
template <>
inline std::string extractTag(const Type&, const char *key)
{
if (!strcmp(key, "blah-int"))
return "1";
if (!strcmp(key, "blah-unsigned-int"))
return "-should-not-be-reached-";
if (!strcmp(key, "blah-bool"))
return "true";
return "invalid";
}
template <>
inline bool extractTag(const Type&, const char *key)
{
if (!strcmp(key, "blah-bool"))
std::cout << "this type-bool function SHOULD BE called" <<
std::endl;
return true;
}
int main(int argc, char *argv[])
{
Type t;
std::cout << extractTag<int>(t, "blah-int") << std::endl;
std::cout << extractTag<unsigned int>(t, "blah-unsigned-int") <<
std::endl;
std::cout << extractTag<bool>(t, "blah-bool") << std::endl;
return 0;
}