deducing function from template parameters

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;
}
 
V

Victor Bazarov

mathieu said:
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 ?

Because it is not one of "deducible contexts". You're asking to deduce
the template argument from a member of the template. That context is
not among listed in 14.8.2.4/9.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top