将自动推导的lambda参数用作常量表达式

问题描述 投票:0回答:2

在C ++中,我试图编写类似于boost-mp11的mp_for_each。但是,尽管mp_for_each始终为给定T中的每个mp_list<Ts...>调用提供的函数,但是我试图提出一种解决方案,一旦对函数的运行时调用产生一个值,该遍历将停止遍历if语句中的false

请参见mp_for_each的实现和用法示例:

Implementation on GitHub

Usage example in Boost reference manual

[显然,mp_for_each的实现设法将函数参数作为常量表达式传递,因此使用户可以将其应用于需要常量表达式的位置。虽然我采用了另一种方法,结合了模板尾部递归,但我希望函数参数也可以作为常量表达式传递。但是,GCC抱怨它“不是常量表达式”。

我的代码如下:

#include <cstdlib>
#include <iostream>
#include <typeinfo>
#include <utility>
#include <boost/mp11.hpp>

template<std::size_t T_counter>
struct WhileGreaterZero
{
    template<typename T_Function>
    constexpr WhileGreaterZero(T_Function&& function)
    {
        if (function(T_counter))  // pass function argument
            WhileGreaterZero<T_counter - 1>(std::forward<T_Function>(function));
    }
};

template<>
struct WhileGreaterZero<0>
{
    template<typename T_Function>
    constexpr WhileGreaterZero(T_Function&&) {}
};

int main()
{
    using boost::mp11::mp_at_c;
    using boost::mp11::mp_list;
    using boost::mp11::mp_size;

    using Types = mp_list<bool, int, double>;

    WhileGreaterZero<mp_size<Types>::value - 1>(
        [](auto counter) {  // function parameter
            using Type = mp_at_c<Types, counter>;

            if (typeid(Type) == typeid(int))
                return false;

            return true;
        }
    );
}

使用g ++ 7.4.0编译时,遇到以下错误(格式化为我的喜好):

$ g++ -std=c++17 -I/path/to/boost

wgz.cpp:
    In substitution of ‘
        template<
            class L,
            long unsigned int I
        >
        using mp_at_c =
            typename boost::mp11::detail::mp_if_c_impl<
                (I < typename boost::mp11::detail::mp_size_impl<L>::type:: value),
                boost::mp11::detail::mp_at_c_impl<L, I>,
                void
            >::type::type
        [
            with L = boost::mp11::mp_list<bool, int, double>;
            long unsigned int I = counter
        ]
    ’:

wgz.cpp:42:49:
    required from ‘
        main()::<lambda(auto:1)>
        [with auto:1 = long unsigned int]
    ’

wgz.cpp:14:21:
    required from ‘
        constexpr WhileGreaterZero<T_counter>::WhileGreaterZero(T_Function&&)
        [
            with T_Function = main()::<lambda(auto:1)>;
            long unsigned int T_counter = 2
        ]
    ’

wgz.cpp:49:5:
    required from here

wgz.cpp:42:49:
    error: ‘counter’ is not a constant expression
             using Type = mp_at_c<Types, counter>;
                                                 ^
wgz:42:49:
    note: in template argument for type ‘long unsigned int’

为什么counter在我的代码中不被视为常量表达式?在这方面,mp11的代码和我的代码之间的关键区别是什么?

c++ lambda parameter-passing constexpr boost-mp11
2个回答
1
投票
function(T_counter)

to

function(std::integral_constant< std::size_t, T_counter >{})

0
投票
using Type = mp_at_c<Types, counter>;

您必须等待模板lambda或实现自己的函子

© www.soinside.com 2019 - 2024. All rights reserved.