嵌套lambda中的函数参数的decltype无法编译

时间:2018-11-08 22:23:02

标签: c++ gcc c++17 constexpr decltype

以下代码使用clang 6和7以及g ++ 7.2进行编译。但是g ++ 8.2拒绝了它,抱怨是error: ‘predicate’ is not captured

#include <utility>
#include <iostream>

template<bool b>
constexpr auto bool_c = std::bool_constant<b>{};

template<char...  str>
struct string {
};

template<typename T>
constexpr bool is_bool_constant_like_v = (std::is_base_of_v<std::bool_constant<true>, T> ||
                                          std::is_base_of_v<std::bool_constant<false>, T>);

constexpr auto index_if = [](auto predicate, auto &&tup) {
    using predicate_t = decltype(predicate);
    constexpr auto func = [](auto x) {
        using predicate_return_t = decltype(predicate(std::declval<decltype(x)>()));
        if constexpr (is_bool_constant_like_v<predicate_return_t>) {
            return bool_c<predicate_return_t::value>;
        }
    };
    // actual code here callinf func....
    return 42;
};

int main() {
#ifdef __GLIBCPP__
    std::printf("GLIBCPP: %d\n",__GLIBCPP__);
#endif
#ifdef __GLIBCXX__
    std::printf("GLIBCXX: %d %d\n", _GLIBCXX_RELEASE, __GLIBCXX__);
#endif
    //return 0;
    constexpr auto abababa = string<'a', 'b', 'a', 'b', 'a'>{};
    constexpr auto xxx = index_if([](auto x) constexpr { return bool_c<true>; }, abababa);
}

问题似乎出在L 18的predicate内使用decltype(),我想这是g ++中的错误,还是其他编译器错了?

有趣的是,如果我向嵌套的lambda添加一个=默认捕获,它再次无法在g ++-8.2中编译,但现在抱怨predicate is not a constant expression

使用decltype(std::declval<predicate_t>(....)的明显解决方法有效。所以我只想知道哪个编译器做对了?

Live example here

0 个答案:

没有答案