The following program's output seems to contradict itself:
#include <type_traits>
#include <iostream>
#include <functional>
void foo(int&){ std::cout << "called\n"; }
int main() {
int a;
foo(a);
std::cout << std::is_invocable_v<decltype(foo), decltype(a)> << std::endl;
std::invoke(foo, a);
}
The output is:
called
0
called
Which seems to me to be invoking a function that is not invocable? What is going on here?
decltype(a)
is int
. This corresponds to invoking f
with an int
prvalue -- something like f(7)
. That one indeed doesn't compile, because a non- const
lvalue reference cannot bind to a prvalue.
What you're doing instead in main
is calling f
with an lvalue , a
, to which the reference can bind just fine.
To get the correct result from std::is_invocable
, use the expression form of decltype
by adding parentheses:
std::is_invocable_v<decltype(foo), decltype((a))>
// ^ ^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.