C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

automatic return value deduction of function templates

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Thomas Mang
Guest





PostPosted: Tue May 22, 2007 12:11 am    Post subject: automatic return value deduction of function templates Reply with quote



Hi,


Given is this very simple template function:

template <class T, class U>
resultType foo(T v1, U v2) {
return v1 + v2;
}

And the question:
What is the best way to determine what resultType should be?

Using the normal type-conversion rules, if say T is an int and U is a
double, the result of the expression of v1 + v2 is of course double, so
ideally double should be returned. But how can I code this in returnValue?


Of course, I can write something along the lines of:

template <class T, class U>
struct builtinReturnValue;

and specialize that, such as:

template <>
struct builtinResultType<int, double>{
typedef double result_type;
};

and use that.
Does a thing doing this already exist in a library? (boost::result_of
does not seem to do that job)

Furthermore, if I take overloaded operators into account, ideally that
should work too.


My question is probably closely related to N1454:
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1454.html

Once T and U are deduced, the compiler is clearly able to determine the
type of the expression in the return - statement. Is a future version of
the std likely to support access to that information - it would be kind
of the auto-proposal for return-types, something along these lines:

// example pseudo code:

template <class T, class U>
auto foo(T v1, U v2) {
return v1 + v2
}


??

thanks,
Thomas



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.