Since I had to write some C++14 compatible code where I still wanted to make use of std::is_invocable
in a template I tried to use boost::callable_traits::is_invocable
. However while boost::callable_traits::is_invocable
returned similar results, boost::callable_traits::is_invocable_v
did not.
这是行为的一个简单示例:
#include <iostream>
#include <boost/callable_traits.hpp>
#include <type_traits>
#include <boost/version.hpp>
int test(int){
return 0;
}
int main()
{
std::cout << "Boost version: " << BOOST_LIB_VERSION << std::endl;
std::cout<< "boost::callable_traits::is_invocable_v: "
<< boost::callable_traits::is_invocable_v<decltype(test), int> << std::endl;
std::cout<< "boost::callable_traits::is_invocable: "
<< boost::callable_traits::is_invocable<decltype(test), int>::value << std::endl;
std::cout<< "std::is_invocable_v: "
<< std::is_invocable_v<decltype(test), int> << std::endl;
std::cout<< "std::is_invocable: "
<< std::is_invocable<decltype(test), int>::value << std::endl;
return 0;
}
结果是:
Boost version: 1_72
boost::callable_traits::is_invocable_v: 0
boost::callable_traits::is_invocable: 1
std::is_invocable_v: 1
std::is_invocable: 1
I uploaded the code to wandbox aswell.
这种令人惊讶的行为的原因是什么?
This looks like a Boost bug to me.
is_invocable
andis_invocable_v
have different implementations (taken from Boost 1.69, comment is mine):I guess
detail::traits<T>
should be justT
. In your particular example,detail::traits<T>
isdetail::function<int(int)>
. Its invocability has nothing to do with invocability oftest
.