Compile-time introspection of free-floating functions, does this work?
I think I might have come up with a way to detect at runtime if a
free-floating function has been defined or not, and avoid referencing
it if it isn't. More concretely, in a project of mine I need to know
if std::strtold exists in <cstdlib> or not, and call it only if it does
(and if it doesn't, fall back to calling std::strtod).
What's the jury's opinion of this? Standard, non-standard, ill-formed,
suspicious? Any suggestions to make it better?
//---------------------------------------------------------------------
#include <iostream>
#include <cstdlib>
namespace std
{
template<typename T1, typename T2>
char strtold(T1, T2);
}
namespace
{
const bool hasStrtold =
(sizeof(std::strtold("0", (char**)0)) > 1);
template<bool> struct Func;
template<> struct Func<false>
{
static void func() { std::cout << "No strtold().\n"; }
};
template<> struct Func<true>
{
static void func()
{
std::cout << "Has strtold(). Test: "
<< std::strtold("12.3", (char**)0) << "\n";
}
};
void func()
{
Func<hasStrtold>::func();
}
}
int main()
{
func();
}
//---------------------------------------------------------------------