Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why doesn't std::is_invocable_r_v<void, TFun> return false if the function does not return void

See the test case

#include <iostream>
#include <type_traits>


int main(){
    const char* hw = "hello world";
    auto task = [hw] ()->const char * { return hw ; };

    std::cout << std::is_invocable_r_v<const char *, decltype(task)> << std::endl;
    std::cout << std::is_invocable_r_v<void, decltype(task)> << std::endl;
    std::cout << std::is_invocable_r_v<const char, decltype(task)> << std::endl;
}

Result is

1
1
0

but I would expect

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1
0
0

See

https://godbolt.org/z/d1cbfbsEY

>Solution :

Why doesn’t std::is_invocable_r_v<void, TFun> return false if the function does not return void

From https://en.cppreference.com/w/cpp/types/is_invocable I see that:

  1. Determines whether INVOKE<R>(std::declval<Fn>(), std::declval<ArgTypes>()...) is well formed when treated as an unevaluated operand.

Then I click on "INVOKE" and jump to https://en.cppreference.com/w/cpp/utility/functional which has:

The exposition-only operation INVOKE<R>(f, t1, t2, ..., tN) is defined as static_cast<void>(INVOKE(f, t1, t2, ..., tN)) if R is (possibly cv-qualified) void, otherwise INVOKE(f, t1, t2, …, tN) implicitly converted to R.

Surely static_cast<void>(task()) is fine, void just discard the value. std::is_invocable_r_v<void, decltype(task)> should be true.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading