提高is_invocable_v和is_invocable :: value返回不同​​的结果

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 and is_invocable_v have different implementations (taken from Boost 1.69, comment is mine):

template<typename T, typename... Args>
struct is_invocable : detail::is_invocable_impl<T, Args...>::type {
    using type = typename detail::is_invocable_impl<T, Args...>::type;
};

template<typename T, typename... Args>
constexpr bool is_invocable_v =
    detail::is_invocable_impl<detail::traits<T>, Args...>::type::value;

//                            ^^^^^^^^^^^^^^^^^ Why?

I guess detail::traits<T> should be just T. In your particular example, detail::traits<T> is detail::function<int(int)>. Its invocability has nothing to do with invocability of test.

点赞
评论