I have a function foo()
that is being provided in a library context. The library defines a handful of overloads for this function, like:
char foo(float x, int y);
short foo(double x, char y);
(I made the above argument/result types up. The takeaway is that there isn't a generic relationship between the argument types and the overload's corresponding return type.)
The idea is that the library user can add overloads for foo()
for their own user-defined types as needed. Function overloading accomplishes this very easily.
I would like to make the foo()
family of functions usable in a Boost.Proto expression. In order to do so, I think I would need to wrap the above in a function object with a template call operator:
struct foo_wrap
{
template <typename A1, typename A2>
result_type operator()(A1 a1, A2 a2) { return foo(a1, a2); }
};
The problem comes with how to define result_type
. I realize this would be easy with C++11 and decltype()
and trailing function return types, but I'm looking for a C++03 solution. Therefore, foo_wrap
needs to be a TR1-style function object. I need to find a way to define result_type
as a compile-time function of the argument types A1
and A2
. This is needed not only for the return type of operator()
, but also for the TR1 result_of
protocol to work as well. In short:
You may feed manually a traits for that:
template <typename A1, typename A2>
struct foo_wrap_result;
with
struct foo_wrap
{
template <typename A1, typename A2>
typename foo_wrap_result<A1, A2>::type
operator()(A1 a1, A2 a2) const { return foo(a1, a2); }
};
And specialization of the traits:
template <>
struct foo_wrap_result<float, int> { typedef char type; };
template <>
struct foo_wrap_result<double, char> { typedef short type; };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With