On 21.08.2015 21:11, Doug Mika wrote:
> Hi, why must I write:
> std::result_of<decltype(fn)&(int)>::type b; //what is the & for?
> and why can't I write this instead:
> std::result_of<fn(int))>::type b; //what is wrong with this?
Well, first off, it's self-inflicted pain to use `result_of` directly
here. Instead write just
function<decltype(fn)>::result_type b;
where `function` is `std::function`.
You need the `decltype` to get the /type/ of the function `fn`.
With direct use of `result_of` the type that you supply is a
construction that communicates two things simultaneously to `result_of`:
* the full function type, or more generally a /callable/ type, that
you're interested in result type of, call that A, and
* the argument types that it should be regarded as called with, call
them B1, B2, ... Bn,
Typically you communicate this as A*(B1, B2, ... Bn), which is the type
of a function returning A* and taking arguments B1, B2 ... Bn. In your
code you instead used A&(B1, B2, ... Bn). `result_of` accepts both
variants, but A(B1, B2, ... Bn) can't be used, because functions are not
copyable or movable; you can't have a function returning a function.
I don't know why it's done that way for `result_of`. It's inordinately
complicated and just hackish, it doesn't make much sense as client code
interface, and as `std::function` proves it's not necessary. I suspect
that it's due to some internal requirements in early Boost library code.
> in my program:
> // result_of example
> #include <iostream>
> #include <type_traits>
>
> int fn(int a) {return a;} // function
>
> int main() {
> int a=3;
> //std::result_of<fn(int))>::type b;
> std::result_of<decltype(fn)&(int)>::type b;
> b=5;
> std::cout<<b<<std::endl;
> return 0;
> }
>
Cheers & hth.,
- Alf