S
Simon
Hi,
I'm having a problem with templates and specialisation. I'm using it to
overload the same function so it can return different things. I can't see
what I'm doing wrong, although my compiler promises me I am! Here follows an
example of my code, and i've included the error VC is giving me if that
helps. Can anyone point me in the right direction?
Cheers,
Simon ;o)
--
template <class A, class B>
A foo(B x);
template <>
int foo(int x) {
return x + 1;
}
template <>
float foo(int x) {
return (float)(x) + 1.0f;
}
int main () {
int a = 1;
int b = foo<int,int>(a);
float c = foo<float,int>(a);
return 1;
}
VC6 is giving...
c:\test\main.cpp(13) : error C2556: 'float __cdecl foo(int)' : overloaded
function differs only by return type from 'int __cdecl foo(int)'
c:\test\main.cpp(8) : see declaration of 'foo'
c:\test\main.cpp(13) : error C2371: 'foo' : redefinition; different basic
types
c:\test\main.cpp(8) : see declaration of 'foo'
c:\test\main.cpp(21) : error C2893: Failed to specialize function template
'A __cdecl foo(B)'
With the following template arguments:
'int'
'int'
Error executing cl.exe.
I'm having a problem with templates and specialisation. I'm using it to
overload the same function so it can return different things. I can't see
what I'm doing wrong, although my compiler promises me I am! Here follows an
example of my code, and i've included the error VC is giving me if that
helps. Can anyone point me in the right direction?
Cheers,
Simon ;o)
--
template <class A, class B>
A foo(B x);
template <>
int foo(int x) {
return x + 1;
}
template <>
float foo(int x) {
return (float)(x) + 1.0f;
}
int main () {
int a = 1;
int b = foo<int,int>(a);
float c = foo<float,int>(a);
return 1;
}
VC6 is giving...
c:\test\main.cpp(13) : error C2556: 'float __cdecl foo(int)' : overloaded
function differs only by return type from 'int __cdecl foo(int)'
c:\test\main.cpp(8) : see declaration of 'foo'
c:\test\main.cpp(13) : error C2371: 'foo' : redefinition; different basic
types
c:\test\main.cpp(8) : see declaration of 'foo'
c:\test\main.cpp(21) : error C2893: Failed to specialize function template
'A __cdecl foo(B)'
With the following template arguments:
'int'
'int'
Error executing cl.exe.