lundi 1 novembre 2021

How to correctly use invoke_result_t?

I am having an issue with type traits that I don't understand, I have created the below minimal example. Why does the second call to foo not compile? It gives the error:

                 from C:/msys64/mingw64/include/c++/10.3.0/bits/nested_exception.h:40,
                 from C:/msys64/mingw64/include/c++/10.3.0/exception:148,
                 from C:/msys64/mingw64/include/c++/10.3.0/ios:39,
                 from C:/msys64/mingw64/include/c++/10.3.0/ostream:38,
                 from C:/msys64/mingw64/include/c++/10.3.0/iostream:39,
                 from invoke_result_test.cpp:1:
C:/msys64/mingw64/include/c++/10.3.0/type_traits: In substitution of 'template<class _Fn, class ... _Args> using invoke_result_t = typename std::invoke_result::type [with _Fn = main()::<lambda(bool&)>; _Args = {bool}]':
invoke_result_test.cpp:6:11:   required from here
C:/msys64/mingw64/include/c++/10.3.0/type_traits:2957:11: error: no type named 'type' in 'struct std::invoke_result<main()::<lambda(bool&)>, bool>'
 2957 |     using invoke_result_t = typename invoke_result<_Fn, _Args...>::type;

Reading around it looks like that the function is not callable so invoke_result has no type to return. I would like to be able to call foo with references so I can return values and non-references but cant work this out. Is this possible? I assume so as STL code manages this. What have I not understood?

Minimal Code:

#include <type_traits>

template <typename F, 
          typename... A, 
          typename R = std::invoke_result_t<std::decay_t<F>, std::decay_t<A>...>>
R foo(const F& aF, const A& ...aA)

{
    return aF(aA...);
}

int main()
{
    bool positive = true;
    std::cout << foo([](bool flag) -> int
                        {
                            if (flag) return 1;
                            return -1;
                        },
                        positive);
                        
    std::cout << foo([](bool& flag) -> int
                        {
                            flag = !flag;
                            if (flag) return 1;
                            return -1;
                        },
                        positive);
                        
}

Thanks.

Aucun commentaire:

Enregistrer un commentaire