Default Template Value

M

Michael DeWulf

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

amparikh

Michael said:
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.
 

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
474,262
Messages
2,571,054
Members
48,769
Latest member
Clifft

Latest Threads

Top