I have following problem:
I want to determine between two types without actually evaluating the ‘resulting’ types – since the type may not exist at all – be invalid. (Please no C++11 stuff)
Example:
#include <iostream>
#include <iterator>
template <bool B, typename T, typename F>
struct TemplateIf {
};
template <typename T, typename F>
struct TemplateIf<true, T, F> {
typedef T Result;
};
template <typename T, typename F>
struct TemplateIf<false, T, F> {
typedef F Result;
};
int main(int argc, char** argv)
{
// On GCC this is error as std::iterator_traits<int>::value_type doesn't exist
typename TemplateIf<true, int, std::iterator_traits<int>::value_type >::Result a;
a = 5;
std::cout << a << std::endl;
return 0;
}
Can it somehow be determined? (Assuming that chosen type is always valid, but not chosen type maybe invalid).
Instead of passing the types directly, pass a metafunction which evaluates to the types. This metafunction can then be evaluated lazily within the if.