简体   繁体   中英

Why am i getting 2 as an output?

#include<iostream>
using namespace std;
template<class T>
void f(T &i)
{
    cout<<"1";
}
void f(const int&i)
{
    cout<<"2";
}
int main()
{
    f(7);
}

I have used a template function and a normal function. But the function with const int argument is executing when called. Why is that so?

When your code is compiling, depend on type of arguments and number of argument, compiler will find the most related function to response your function call f(7) before move to Template functions. In this case, the most related function is void f(const int&i) so you got 2 as an output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM