为什么这个 boost mp11 mp_count_if_q 代码总是 return 0?

Why does this boost mp11 mp_count_if_q code always return 0?

我想知道为什么下面的代码不能按预期工作(is_numeric 总是 0)。

#include <type_traits>
#include <utility>
#include <boost/mp11/algorithm.hpp>
#include <boost/mp11/bind.hpp>
#include <boost/mp11/tuple.hpp>

using namespace boost::mp11;

using NumericTypes = std::tuple<short, unsigned short, int, unsigned int, 
                                long int, unsigned long int, 
                                long long int, unsigned long long int, 
                                float, double, long double>;
template<typename T>
static constexpr int is_numeric = mp_count_if_q<NumericTypes,
mp_bind_front<std::is_same,std::remove_cvref<T>>>::value;

int main(){
    return is_numeric<char>+2*is_numeric<int>;
}

我的假设是,在尝试编译时我犯了一些愚蠢的错误(我使用 _q 版本只是因为我无法让 mp_count_if 工作),但我没有看到任何明显的东西,从什么我发现 tests/doc 不包含与我相对复杂的示例类似的内容。

FWIW bind front 似乎像我预期的那样工作...

template<typename T>
using is_int = mp_bind_front<std::is_same,int>::fn<T>;
static_assert(!is_int<float>());
static_assert(is_int<int>());

您正在与 std::remove_cvref<T> 进行比较,它本身是一种类型,而不是数字。但我假设您想针对特征生成的类型进行测试:std::remove_cvref<T>::type,或更好的 std::remove_cvref_t<T>.

现在

template<typename T>
static constexpr int is_numeric = mp_count_if_q<NumericTypes,
mp_bind_front<std::is_same,std::remove_cvref_t<T>>>::value;

按预期工作!