简体   繁体   中英

boost is_invocable_v and is_invocable::value returning different results

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.

Here is a simple example of the behaviour:

#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;
}

The result is:

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.

Whats the reason for this surprising behaviour?

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 .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM