By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

Default Template Value

P: n/a
Just to help my understanding, what is supposed to happen on a template
function or class when the user does not specify the template type?

GCC reports that the code below is using min_t with the form:

N min_t(N, T) [with N = int, T = double]

Which are the types of the paramters i and j. So does the compiler just
use its type knowledge to create the "proper" instance of the function?
If so, since that is what it seems to be doing, is this part of the
official C++ standard?

Thanks,
Mike
------CODE------

#include <iostream>

using namespace std;

template<class N, class T>
N min_t(N first, T second)
{
if(first < second)
{
return first;
}
else
{
return second;
}
}

int main(int argc, char ** argv)
{
int i = 3;
double j = -1;
double k = min_t(i,j);
std::cout << k << std::endl;
return 0;
}

Sep 15 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

Michael DeWulf wrote:
Just to help my understanding, what is supposed to happen on a template
function or class when the user does not specify the template type?

GCC reports that the code below is using min_t with the form:

N min_t(N, T) [with N = int, T = double]

Which are the types of the paramters i and j. So does the compiler just
use its type knowledge to create the "proper" instance of the function?
If so, since that is what it seems to be doing, is this part of the
official C++ standard?

Thanks,
Mike
------CODE------

#include <iostream>

using namespace std;

template<class N, class T>
N min_t(N first, T second)
{
if(first < second)
{
return first;
}
else
{
return second;
}
}

int main(int argc, char ** argv)
{
int i = 3;
double j = -1;
double k = min_t(i,j);
std::cout << k << std::endl;
return 0;
}
Lookup "template argument deduction".
It is the most extensive topic when it comes to function templates.

Sep 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.