Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why template by return value is forbidden?

Reply
Thread Tools

Why template by return value is forbidden?

 
 
PLM
Guest
Posts: n/a
 
      08-10-2005
Hello, all.
I should lke to declare template function as following:

template <class TYPE>
TYPE Test() {return (TYPE)5; }

void main(void) {
int i; float f;
i = Func();
f = Func();
}

What is wrong with this?
What I receive from gcc is :

main.cpp:28: no matching function for call to `Func()'


Thanks ahead
Leon

 
Reply With Quote
 
 
 
 
msalters
Guest
Posts: n/a
 
      08-10-2005

PLM schreef:

> Hello, all.
> I should lke to declare template function as following:
>
> template <class TYPE>
> TYPE Func() {return (TYPE)5; } // c/p error fixed - MSA
>
> void main(void) {
> int i; float f;
> i = Func();
> f = Func();
> }
>
> What is wrong with this?
> What I receive from gcc is :
>
> main.cpp:28: no matching function for call to `Func()'


The definition is completely legal. However, you must specify
TYPE. You simply cannot deduce TYPE from the =, because the overload
of operator= is selected based on TYPE. C++ resolves these things
from the bottom up. One has to choose a direction when breaking
such logical cycles.

So, write i = Func<int>(); or
template <class TYPE>
void Func( TYPE& t ) { t = 5; }
Func( i );

HTH,
Michiel Salters

 
Reply With Quote
 
 
 
 
Mario Fratelli
Guest
Posts: n/a
 
      08-10-2005
Hi,

of course, I suppose that you meant to call Test(), since Func() is not
declared.

Template matching is based on function overloading matching.

Function overloading doesn't work for return types, but only for
parameter's types.

You have to specify the template specialization like this:

i = Func<int>();

Ciao,
Mario Fratelli.

 
Reply With Quote
 
PLM
Guest
Posts: n/a
 
      08-10-2005
msalters wrote:

>
> PLM schreef:
>
>> Hello, all.
>> I should lke to declare template function as following:
>>
>> template <class TYPE>
>> TYPE Func() {return (TYPE)5; } // c/p error fixed - MSA
>>
>> void main(void) {
>> int i; float f;
>> i = Func();
>> f = Func();
>> }
>>
>> What is wrong with this?
>> What I receive from gcc is :
>>
>> main.cpp:28: no matching function for call to `Func()'

>
> The definition is completely legal. However, you must specify
> TYPE. You simply cannot deduce TYPE from the =, because the overload
> of operator= is selected based on TYPE. C++ resolves these things
> from the bottom up. One has to choose a direction when breaking
> such logical cycles.
>
> So, write i = Func<int>(); or
> template <class TYPE>
> void Func( TYPE& t ) { t = 5; }
> Func( i );
>
> HTH,
> Michiel Salters

Thank you for reply.
Yes, I understand that i = Func<int>() will work, but this is exactly I
wanted to avoid...)
Now, please, can you be so kind to detail your explanation about the
direction? I did not catch...(

 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      08-10-2005
> Yes, I understand that i = Func<int>() will work, but this is exactly I
> wanted to avoid...)


Why do you want to avoid it? If Func is to return an object of some type,
then you need to specify what that type is. It is only when that is
determined the compiler would worry about how to convert the return type for
the assignment operation.

One way to work around is to not use the return value mechanism. Pass the
variable by reference:

template <typename T>
Func(T& t)
{
t = T(5);
}

> Now, please, can you be so kind to detail your explanation about the
> direction? I did not catch...(
>


Consider:

class A{};
class B{};

A& operator = (A&, const B&);
B& operator = (B&, const A&);

template <typename T>
T Func(void);

A a;
a = Func(); // problem line

In the last line (commented problem line), the compiler has serveral
options, neither is much better than the other and thus would raise
ambiguity. In fact, the compiler never knows ahead about the conversion so
it simply can't depend on the return type to match functions (and function
templates).


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      08-10-2005

"PLM" <(E-Mail Removed)> wrote in message
news:1123666994.a7cd07df2ba8be9f1c731a0d124d6dd4@t eranews...
> Hello, all.
> I should lke to declare template function as following:
>
> template <class TYPE>
> TYPE Test() {return (TYPE)5; }
>
> void main(void) {


int main(void) {

> int i; float f;
> i = Func();
> f = Func();'


i = Test<int>();
f = Test<float>();

-Mike


 
Reply With Quote
 
Martin Vorbrodt
Guest
Posts: n/a
 
      08-10-2005
I think template cast operator would help...
Create a small class which encapsulates value of 5:

static class {
public:
template<typename T>
operator T() { return static_cast<T>(5); }
} five = {};

#include <iostream>
using std::cout;
using std::endl;

int main() {
float valuef = five;
int valuei = five;

cout << valuef << endl;
cout << valuei << endl;

return 0;
}

Hope that helps!
Martin



"PLM" <(E-Mail Removed)> wrote in message
news:1123666994.a7cd07df2ba8be9f1c731a0d124d6dd4@t eranews...
> Hello, all.
> I should lke to declare template function as following:
>
> template <class TYPE>
> TYPE Test() {return (TYPE)5; }
>
> void main(void) {
> int i; float f;
> i = Func();
> f = Func();
> }
>
> What is wrong with this?
> What I receive from gcc is :
>
> main.cpp:28: no matching function for call to `Func()'
>
>
> Thanks ahead
> Leon
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Can a template function return a template type? Damon C++ 2 12-15-2003 12:56 PM
Return a return value from Perl to Javascript PvdK Perl 0 07-24-2003 09:20 AM



Advertisments