template return type

Hi All


I'm a little confused...

Say I have thr following function declaration
1
2
template <class T>
T& f(int x);


When I want to execute it like this
1
2
3
4
5

int main()
{
    f(3); 
}


does not compile

but

1
2
3
4
int main()
{
    f<float>(3); 
}


does compile. Why? and how can I implemet shuch a function?

Thank u in advance
Eyal
Well think about how will the compiler know what is the return type of the function when you call it as f( 3 )?
I don't see how it knows what the return even when writting
1
2
3

f<float>(3);


lets say for example the f takes int and return it twice the value how to implement it like the above declaration?

Thx
When you have written f<float>(3); you have explicitly specified that the template type parameter T corresponds to float. So the compiler will be happy.:)
Last edited on
I see now.

Thank u for ur help :)
Topic archived. No new replies allowed.