【问题标题】:Why does this boost mp11 mp_count_if_q code always return 0?为什么这个 boost mp11 mp_count_if_q 代码总是返回 0?
【发布时间】:2020-07-02 06:14:35
【问题描述】:

我想知道为什么下面的代码不能按预期工作(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 工作),但我没有看到任何明显的东西,并且从我发现的测试/文档中不包含类似于我相对复杂的示例的内容。

FWIW 绑定前端似乎可以正常工作...

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>());

【问题讨论】:

    标签: c++ c++20 boost-mp11


    【解决方案1】:

    您正在与std::remove_cvref&lt;T&gt; 进行比较,它本身是一种类型,理所当然地不是数字。但我假设您想针对从 trait 产生的类型进行测试:std::remove_cvref&lt;T&gt;::type,或者更好的是 std::remove_cvref_t&lt;T&gt;

    现在

    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;
    

    按预期工作!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 2021-01-24
    • 2012-05-09
    • 2017-03-28
    • 2016-09-16
    • 2020-05-25
    相关资源
    最近更新 更多