473,587 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Templates and Specialisation error

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.cp p(13) : error C2556: 'float __cdecl foo(int)' : overloaded
function differs only by return type from 'int __cdecl foo(int)'
c:\test\main.cp p(8) : see declaration of 'foo'
c:\test\main.cp p(13) : error C2371: 'foo' : redefinition; different basic
types
c:\test\main.cp p(8) : see declaration of 'foo'
c:\test\main.cp p(21) : error C2893: Failed to specialize function template
'A __cdecl foo(B)'
With the following template arguments:
'int'
'int'
Error executing cl.exe.

Jul 19 '05 #1
12 5245
"Simon" <so*********@no .no> wrote in message
news:bk******** **@titan.btinte rnet.com...
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) {
Whoops! You cannot overload on return type alone.
See below.
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.cp p(13) : error C2556: 'float __cdecl foo(int)' : overloaded
function differs only by return type from 'int __cdecl foo(int)'
This is an accurate diagnosis. Functinos cannot be
overloaded on return type only. The parameter types
and/or count must differ.
c:\test\main.cp p(8) : see declaration of 'foo'
c:\test\main.cp p(13) : error C2371: 'foo' : redefinition; different basic
types
You're trying to define a function more than
once, with a different return type. Not allowed.
The parameter list must differ. Return type does
not affect overloading.
c:\test\main.cp p(8) : see declaration of 'foo'
c:\test\main.cp p(21) : error C2893: Failed to specialize function template
'A __cdecl foo(B)'
With the following template arguments:
'int'
'int'
Error executing cl.exe.


-Mike
Jul 19 '05 #2
Simon wrote:
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)


VC6 is of a vintage that has alot of problems with templates.

Get VC7.1

Jul 19 '05 #3
WW
Gianni Mariani wrote:
Simon wrote:
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)


VC6 is of a vintage that has alot of problems with templates.

Get VC7.1


Other than that can you tell what rule does it get wrong in this case? Or
just guessing?

--
WW aka Attila
Jul 19 '05 #4

"Gianni Mariani" <gi*******@mari ani.ws> wrote in message
news:bk******** @dispatch.conce ntric.net...
Simon wrote:
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)
VC6 is of a vintage that has alot of problems with templates.


Not in this case. The code is invalid.


Get VC7.1


That won't help (unless they broke it so it
accepts the invalid code -- which isn't much of
a solution. :-))

-Mike
Jul 19 '05 #5
WW
Mike Wahler wrote:
VC6 is of a vintage that has alot of problems with templates.


Not in this case. The code is invalid.


Get VC7.1


That won't help (unless they broke it so it
accepts the invalid code -- which isn't much of
a solution. :-))


I have copy pasted the code into Comeau online and it did not complain. Did
I do something wrong? IMHO those might not be overloads (at least AIR they
aren't) but full specializations . I might of course be wrong. That is why
I have asked for standard text.

--
WW aka Attila
Jul 19 '05 #6
Mike Wahler wrote:
"Gianni Mariani" <gi*******@mari ani.ws> wrote in message
news:bk******** @dispatch.conce ntric.net...

....

VC6 is of a vintage that has alot of problems with templates.

Not in this case. The code is invalid.


Then gcc 3.3.1 is broken. It compiled it without complaint.

So who is right ?

Jul 19 '05 #7
WW
Gianni Mariani wrote:
Mike Wahler wrote:
"Gianni Mariani" <gi*******@mari ani.ws> wrote in message
news:bk******** @dispatch.conce ntric.net...

...

VC6 is of a vintage that has alot of problems with templates.

Not in this case. The code is invalid.


Then gcc 3.3.1 is broken. It compiled it without complaint.

So who is right ?


The standard. That is why I have asked for it. IMO the guessing you did is
very dangerous. You have stated something and you do not really know if it
is true or not. :-(

I _think_ that if you fully specialize a template it does _not_ become an
overload. Just think about name mangling (which is an implementation
detail, but helps understanding). The mangled name of a function template
specialization will contain all the names (or identifiers or whatevers) of
the types used to specialize it. Including the dependent return type. Now
IIRC MS 6 does it worng. It does not put into the mangled name types which
are not part of the argument list. MS has tried there to mix overload
resolution with templates. Which did not work:

template <class T> void f() {
#1
}
f<int>();
f<std::string>( );

Both will call f<int> or f<std::string> , depending on the current phase of
the moon and some other factors.

So it is highly likely that in this case MS is wrong and all the others are
right. But... which part of the standard does it say so? Or is this a grey
area? (I doubt it).

--
WW aka Attila
Jul 19 '05 #8
In article <bk**********@t itan.btinternet .com>, so*********@no. no
says...
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.
You're not doing overloading -- you're doing explicit specialization.
If you tried to do this via overloading, it wouldn't be allowed. You
can't create overloads that differ only in return type, but you CAN
create explicit specializations that do so (though when you do so, the
compiler generally will NOT be able to deduce the type of the parameter
specifying the function's return type).
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?


I believe the code is well-formed, and _should_ be accepted by a
properly functioning compiler. I'm not surprised that quite a few
compilers reject it though, and quite frankly, I'd tend to do the same.
IMO, if you use this, confusion is inevitable while utility is
questionable.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #9
> > I'm having a problem with templates and specialisation. I'm using it to
overload the same function so it can return different things.
You're not doing overloading -- you're doing explicit specialization.
If you tried to do this via overloading, it wouldn't be allowed. You
can't create overloads that differ only in return type, but you CAN
create explicit specializations that do so.


Yeah, sorry. I shouldn't really have used the word overload i guess. I'm
trying to use templates for exactly the reason you suggest, it just is meant
to feel like overloading. e.g. cast<template specialisation to>(overloaded
types from)
(though when you do so, the
compiler generally will NOT be able to deduce the type of the parameter
specifying the function's return type).


I don't understand this comment. Could you expand.
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?


I believe the code is well-formed, and _should_ be accepted by a
properly functioning compiler.


So can we confirm that the original code was correct. There seem to be a
number of people saying it is incorrect, and if so I want to understand
where my misunderstandin g of C++/templates lies.

Thanks for all the help so far,
Simon ;o)
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6466
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a...
2
4771
by: Michael Stembera | last post by:
I would like to use default parameters in nested templates but MS VC++ 7.1 chokes on it. Does anyone know how to fix the simple example below or if indeed it is possible? template <int N=7> class A { };
4
2575
by: SainTiss | last post by:
Hi, From what I've read in several places, it seems that explicit specialization of member functions of class templates is allowed, but partial specialization isn't: template<class T, class R> class A { void foo(); }
6
2228
by: vch | last post by:
When defining templates, can I cound on the compiler to parse only those templates that are actually used? For example, in the following definitions: <code> template <class T> struct Conv {
4
2278
by: Erik Wikström | last post by:
In school (no I will not ask you to do my schoolwork for me) we talked about policy-based design and got an assignment where we got the a code- fragment from a stack-implementation. The idea with the code was that if, when its destructor was called, it still contained any elements it would run 'delete' on them if they were pointers and do...
4
1947
by: KK | last post by:
Hello all, I have class 'atr' which is based on templates. My idea was to initialize it two scenarios 1. std::string case 2. other data types I have defined the Init for the above different cases accordingly (code shown below). However, I get compiler error. Please help me resolve it. Thanks.
11
2314
by: Elpoca | last post by:
Hi: What rules govern the inlining of templated functions and templated class methods? It has always been my understanding that both templated functions and templated class methods were always expanded inline. Recently, I replaced an explicitly written function with one implemented using templates (and partial-template specialisation),...
5
1325
by: er | last post by:
i have class Impl; //abstract class Impl_A:public Impl{...};//abstract class Impl_B:public Impl{...};//abstract class Impl_A_0: public Impl_A{/* implements */}; class S{ public: S(Impl*,...); ...}; class S_A: public S{ public: S_A(Impl_A* impl,...):S(impl){}; class S_B: public S{ public: S_B(Impl_B* impl,...):S(impl){};
2
1302
by: krchandu | last post by:
inline int const& max (int const& a, int const& b) { cout << "Normal function called \n"; return a<b?b:a; } // maximum of two values of any type template <typename T> inline T const& max (T const& a, T const& b) {
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.