Thomas Mang Guest
|
Posted: Tue May 22, 2007 12:11 am Post subject: automatic return value deduction of function templates |
|
|
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! ] |
|