Why template by return value is forbidden?

P

PLM

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
 
M

msalters

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
 
M

Mario Fratelli

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.
 
P

PLM

msalters said:
PLM schreef:


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...:-((
 
B

benben

Yes said:
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).
 
M

Mike Wahler

PLM said:
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
 
M

Martin Vorbrodt

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top