473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

deducing function from template parameters

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

Dec 12 '06 #1
1 1275
mathieu wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 12 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
2
by: baumann | last post by:
hi all, i am reading the C++ templates complete guide, i have question on the following statement. We must therefore make sure the template parameters of the class template appear in the type...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
4
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax)...
8
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
12
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.