| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"msalters" |
| Date: |
10 Aug 2005 04:49:02 AM |
| Object: |
Re: Why template by return value is forbidden? |
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
.
|
|
| User: "PLM" |
|
| Title: Re: Why template by return value is forbidden? |
10 Aug 2005 05:28:18 AM |
|
|
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...:-((
.
|
|
|
| User: "benben" |
|
| Title: Re: Why template by return value is forbidden? |
10 Aug 2005 06:40:23 AM |
|
|
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).
.
|
|
|
|
|

|
Related Articles |
|
|