M
mathieu
Hello,
I was playing with template programing and I am struggling with the
following (*):
float f3 = foo((TypeToType<0>::Type*)0);
where foo is declared as:
template <int T>
double foo(typename TypeToType<T>::Type * = 0);
Why is it that my compiler cannot deduce the function from the
parameter I am giving ?
Thanks
Mathieu
(*)
#include <iostream>
// first pass:
template <typename T>
double bar(T * = 0) {
T t = static_cast<T>(1.23);
return t;
}
// second pass:
template <int T> struct TypeToType; // forward declare
template <> struct TypeToType<0>
{ typedef float Type; };
template <> struct TypeToType<1>
{ typedef int Type; };
template <int T>
double foo(typename TypeToType<T>::Type * = 0) {
typedef typename TypeToType<T>::Type type;
type t = static_cast<type>(1.23);
return t;
}
int main()
{
double d1 = bar((float*)0);
double d2 = bar((int*)0);
std::cout << "d1:" << d1 << std::endl;
std::cout << "d2:" << d2 << std::endl;
TypeToType<0>::Type f1 = 1.23f;
TypeToType<1>::Type i1 = 1;
double d3 = foo<0>();
double d4 = foo<1>();
std::cout << "d3:" << d3 << std::endl;
std::cout << "d4:" << d4 << std::endl;
// float f3 = foo((TypeToType<0>::Type*)0);
return 0;
}
I was playing with template programing and I am struggling with the
following (*):
float f3 = foo((TypeToType<0>::Type*)0);
where foo is declared as:
template <int T>
double foo(typename TypeToType<T>::Type * = 0);
Why is it that my compiler cannot deduce the function from the
parameter I am giving ?
Thanks
Mathieu
(*)
#include <iostream>
// first pass:
template <typename T>
double bar(T * = 0) {
T t = static_cast<T>(1.23);
return t;
}
// second pass:
template <int T> struct TypeToType; // forward declare
template <> struct TypeToType<0>
{ typedef float Type; };
template <> struct TypeToType<1>
{ typedef int Type; };
template <int T>
double foo(typename TypeToType<T>::Type * = 0) {
typedef typename TypeToType<T>::Type type;
type t = static_cast<type>(1.23);
return t;
}
int main()
{
double d1 = bar((float*)0);
double d2 = bar((int*)0);
std::cout << "d1:" << d1 << std::endl;
std::cout << "d2:" << d2 << std::endl;
TypeToType<0>::Type f1 = 1.23f;
TypeToType<1>::Type i1 = 1;
double d3 = foo<0>();
double d4 = foo<1>();
std::cout << "d3:" << d3 << std::endl;
std::cout << "d4:" << d4 << std::endl;
// float f3 = foo((TypeToType<0>::Type*)0);
return 0;
}