473,395 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

return type deduction for a function template

Consider the following code

template<class T1, class T2>
T2 sum(T1 a, T1 b)
{
T2 ret = a + b;
return ret;
}

int main()
{
float f1 = 2, f2 = 3;
int i1 = 2, i2 = 4;

sum<float>(f1, f2); //ERROR
float fret = sum<float>(f1, f2); //ERROR
}

Both the statements marked as "ERROR" throw errors during compilation.
Now I can understand the first error in that a return type cannot be
possibly deduced. But what I don't understand is the second error. Why
is the return type not automatically deduced to be float? Such a
deduction happens for arguments of a function template (in case we
don't specify the data type during template instantiation directly).
Then why cannot it happen for the return type? Is it a compiler bug or
was it intended to be the behavior? If so why? I am using g++ compiler
on Linux.
Oct 24 '08 #1
5 6280
SG
On 24 Okt., 07:05, "C++Liliput" <aveekmi...@gmail.comwrote:
Consider the following code

template<class T1, class T2>
T2 sum(T1 a, T1 b)
{
* *T2 ret = a + b;
* *return ret;

}

int main()
{
* *float f1 = 2, f2 = 3;
* *int i1 = 2, i2 = 4;

* *sum<float>(f1, f2); //ERROR
* *float fret = sum<float>(f1, f2); //ERROR
*}
There *is* no type deduction for the return type. But you may do the
following:

template<class T2, class T1// <-- swapped T1 & T2
T2 sum(T1 a, T1 b)
{
T2 ret = a + b;
return ret;
}

int main()
{
float f1 = 2, f2 = 3;

sum<float>(f1, f2); //OK, T2=float, T1=float (deduced)
}

Cheers,
SG
Oct 24 '08 #2
C++Liliput wrote:
Consider the following code

template<class T1, class T2>
T2 sum(T1 a, T1 b)
{
T2 ret = a + b;
return ret;
}

int main()
{
float f1 = 2, f2 = 3;
int i1 = 2, i2 = 4;

sum<float>(f1, f2); //ERROR
float fret = sum<float>(f1, f2); //ERROR
}

Both the statements marked as "ERROR" throw errors during compilation.
Now I can understand the first error in that a return type cannot be
possibly deduced. But what I don't understand is the second error. Why
is the return type not automatically deduced to be float? Such a
deduction happens for arguments of a function template (in case we
don't specify the data type during template instantiation directly).
Then why cannot it happen for the return type? Is it a compiler bug or
was it intended to be the behavior? If so why? I am using g++ compiler
on Linux.
I'm afraid the answer is "because the language was designed to
not to support this". (That it's not impossible to do this can
be seen in other languages.)

Not that commonly it is better to put ineducable template
arguments before those that can be deduced:

template< typename R, typename T >
R sum(T,T);

That way you can still specify the return value but rely on the
compiler to deduce the argument type.

Schobi
Oct 24 '08 #3
On Oct 24, 9:08 am, Hendrik Schober <spamt...@gmx.dewrote:
C++Liliput wrote:
Consider the following code
template<class T1, class T2>
T2 sum(T1 a, T1 b)
{
T2 ret = a + b;
return ret;
}
int main()
{
float f1 = 2, f2 = 3;
int i1 = 2, i2 = 4;
sum<float>(f1, f2); //ERROR
float fret = sum<float>(f1, f2); //ERROR
}
Both the statements marked as "ERROR" throw errors during
compilation. Now I can understand the first error in that a
return type cannot be possibly deduced. But what I don't
understand is the second error. Why is the return type not
automatically deduced to be float? Such a deduction happens
for arguments of a function template (in case we don't
specify the data type during template instantiation
directly). Then why cannot it happen for the return type?
Is it a compiler bug or was it intended to be the behavior?
If so why? I am using g++ compiler on Linux.
I'm afraid the answer is "because the language was designed
to not to support this". (That it's not impossible to do
this can be seen in other languages.)
Other languages are other languages. I'm not sure it would work
that well in C++. (Amongst other things, it would offer yet
another possibility for ambiguity in overload resolution. I
hope you're not going to argue that overload resolution is too
simple in C++, and needs to be made more complicated:-).)

It's possible to do in C++, in specific cases where it makes
sense. Just have the class return a proxy object with a

template< typename T Proxy::operator T() const ;

function.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 24 '08 #4
James Kanze wrote:
On Oct 24, 9:08 am, Hendrik Schober <spamt...@gmx.dewrote:
>C++Liliput wrote:
[...]
>>Both the statements marked as "ERROR" throw errors during
compilation. Now I can understand the first error in that a
return type cannot be possibly deduced. But what I don't
understand is the second error. Why is the return type not
automatically deduced to be float? Such a deduction happens
for arguments of a function template (in case we don't
specify the data type during template instantiation
directly). Then why cannot it happen for the return type?
Is it a compiler bug or was it intended to be the behavior?
If so why? I am using g++ compiler on Linux.
> I'm afraid the answer is "because the language was designed
to not to support this". (That it's not impossible to do
this can be seen in other languages.)

Other languages are other languages. I'm not sure it would work
that well in C++. (Amongst other things, it would offer yet
another possibility for ambiguity in overload resolution. I
hope you're not going to argue that overload resolution is too
simple in C++, and needs to be made more complicated:-).)
I didn't say I want it. I was just rtying to prevent a
useless discussion whether it is, in principle, possible
to allow this in C++. Obviously, I failed... :)
[...]
Schobi
Oct 24 '08 #5
On 2008-10-24 01:05:06 -0400, "C++Liliput" <av********@gmail.comsaid:
Consider the following code

template<class T1, class T2>
T2 sum(T1 a, T1 b)
{
T2 ret = a + b;
return ret;
}

int main()
{
float f1 = 2, f2 = 3;
int i1 = 2, i2 = 4;

sum<float>(f1, f2); //ERROR
float fret = sum<float>(f1, f2); //ERROR
}

Both the statements marked as "ERROR" throw errors during compilation.
Now I can understand the first error in that a return type cannot be
possibly deduced. But what I don't understand is the second error. Why
is the return type not automatically deduced to be float?
Because the compiler doesn't look at how the return type is used to
figure out what it should be. For example,

void f(float);
void f(double);
f(sum<float>(f1, f2));

What should the return type of the template be here?

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 24 '08 #6

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

Similar topics

3
by: Chris Johnson | last post by:
Greetings all: I come across an interesting question (to me anyway) and I do not know the answer. Code/Questions follow: #include <iostream> #if 0 // uncommenting *should* make call...
6
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function...
3
by: Marcel Sebbao | last post by:
I want to use a function object for various callbacks, and also as a normal function. But this function is a template. It is fine, but then I need to specify all the time the template parameter...
5
by: Fabio Fracassi | last post by:
Hi, I belive what i am trying to do is not possible, but I hope someone can change my mind. Here is some code i'd like to write: template <class type> class engine1 {}; template <class...
17
by: benben | last post by:
Given a class template Vector<>, I would like to overload operator +. But I have a hard time deciding whether the return type should be Vector<U> or Vector<V>, as in: template <typename U,...
7
by: Dilip | last post by:
This is just an academic question but is there any particular reason why this does not work? template<typename T> class Foo { public: Foo(T x) : myvar_(x) { } private: T myvar_;
3
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
2
by: Barry | last post by:
The problem brought by one earlier post from comp.lang.c++ http://groups.google.com/group/comp.lang.c++/browse_thread/thread/bf636c48b84957b/ I take part of the question and reproduce the code to...
4
by: KD | last post by:
I have a template function and I'm looking for a way to force the caller to specify the template parameters. In other words, I would like to turn off template parameter deduction. For example, ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.