[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: warning with function templates
From: |
BobR |
Subject: |
Re: warning with function templates |
Date: |
Sun, 17 Jul 2005 00:31:17 GMT |
Christian Christmann wrote in message
<42d902c0$0$18015$9b4e6d93@newsread4.arcor-online.net>...
>Hi,
>
>when I run this code:
>
>#include <iostream>
>using namespace std;
>
>template <class T>
>T GetValue(int a)
>{
> if (a = 1)
> return 1;
> else
> return 2.55; // line 11
>}
>
>int main()
>{
> int a = GetValue<int>(1); // line 16
> cout << "\nInt a : " << a << endl;
>
> double b = GetValue<double>(2);
> cout << "\nDouble b: " << b << endl;
>
> return 0;
>}
>
>I get the gcc warning:
>temp.cpp: In function `T GetValue(int) [with T = int]':
>temp.cpp:16: instantiated from here
>temp.cpp:11: warning: converting to `int' from `double'
>
>Why?
>Line 16 should not be reached when the function is called
>from line 11.
>Is there any way to avoid this warning?
>Thank you
>Chris
You missed a little fine point from Peter J.:
template <class T>
T GetValue(T t) { // <--note the parms
if (t == 1) // <-- note the '==' compare, NOT '=' assignment.
return 1;
else
return 2.55;
}
--
Bob R
POVrookie