简体   繁体   English

是否存在无法避免lambda中的trailing-return-type语法的情况?

[英]Are there cases in which trailing-return-type syntax in lambda cannot be avoided?

关于前一个问题( 是否可以通过引用从lambda返回类型为T的对象而不使用尾随返回类型语法? ),我想知道是否有任何其他重要案例或示例,其中trailing-return-type语法,当使用lambdas时, 无法避免。

I suppose that another case is when there is type inconsistency between differents returns. 我想另一种情况是在不同返回之间存在类型不一致的情况。

A silly example 一个愚蠢的例子

std::function<long(int)> f 
    = [](int v) -> long { if ( v ) return v; else return 0L; };

Obviously you can avoid it if you avoid inconsistency so I don't know if it's significant. 如果你避免不一致,显然你可以避免它,所以我不知道它是否重要。

In C++14, a bit contrived example is the use of sfinae in combination with a generic lambda: 在C ++ 14中,一个有点人为的例子是将sfinae与通用lambda结合使用:

[](auto &&arg)
-> decltype(arg.f(), void())
{ /* do whatever you want */ }

Anyway one could argue that a static_assert suffices: 无论如何,人们可以争辩说static_assert就足够了:

[](auto &&arg) {
    static_assert(has_metod_f<std::decay_t<decltype(arg)>>::value, "!");
    /* do whatever you want */
}

Where has_method_f is the common detector idiom. 其中has_method_f是常见的探测器习语。
Anyway, imagine a case where you want to construct a composed functor starting from a bunch of lambdas: 无论如何,想象一下你想要从一群lambdas开始构造一个组合函子的情况:

#include<utility>
#include<iostream>

template<typename...>
struct Base;

template<typename Func, typename... Others>
struct Base<Func, Others...>: Func, Base<Others...> {
    Base(Func func, Others... others)
    : Func{std::move(func)}, Base<Others...>{std::move(others)...}
    {}

    template<typename... Args>
    auto operator()(int, Args&&... args)
    -> decltype(Func::operator()(std::forward<Args>(args)...)) {
        Func::operator()(std::forward<Args>(args)...);
    }

    template<typename... Args>
    auto operator()(char, Args&&... args) {
        Base<Others...>::operator()(0, std::forward<Args>(args)...);
    }
};

template<>
struct Base<> {
    template<typename... Args>
    auto operator()(Args&&...) {
        std::cout << "fallback" << std::endl;
    }
};

template<typename... Ops>
struct Mixin: Base<Ops...> {
    Mixin(Ops... ops)
        : Base<Ops...>{std::move(ops)...}
    {}

    template<typename... Args>
    auto operator()(Args&&... args) {
        return Base<Ops...>::operator()(0, std::forward<Args>(args)...);
    }
};

struct T { void f() {} };
struct U {};

int main() {
    auto l1 = [](auto &&arg) -> decltype(arg.f(), void()) {
        std::cout << "accept T" << std::endl;
    };

    auto l2 = [](U) {
        std::cout << "accept U" << std::endl;
    };

    Mixin<decltype(l1), decltype(l2)> mixin{std::move(l1), std::move(l2)};
    mixin(T{});
    mixin(U{});
    mixin(0);
}

In this case, a static_assert would prevent the compilation and it is not the expected result. 在这种情况下, static_assert会阻止编译,这不是预期的结果。
On the other side, the trailing return type can be used to enable sfinae directly on the lambdas with the help of their wrappers. 另一方面,尾随返回类型可以用于在包装器的帮助下直接在lambda上启用sfinae。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可能有条件的超载和尾随返回类型? - Conditional overloading with trailing-return-type possible? trailing-return-type中占位符类型的用途是什么? - What is the purpose of a placeholder type in a trailing-return-type? trailing-return-type中的占位符是否会覆盖初始占位符? - Does a placeholder in a trailing-return-type override an initial placeholder? C ++自动函数返回类型的实现[trailing-return-type] - C++ auto function return type implementation [trailing-return-type] 尾随返回类型不是类的完整类上下文是否有特定原因? - Is there a specific reason why a trailing-return-type is not a complete-class context of a class? 如何在C ++ 11之前以尾随返回类型模拟decltype? - How can I simulate decltype in a trailing-return-type before C++11? 标准中的规则在哪里指定具有尾随返回类型的 function 其返回类型包含占位符类型应为“自动” - Where does the rule in the standard specify that a function with a trailing-return-type whose return type contains a placeholder type should be `auto` 是否可以通过lambda引用返回类型为T的对象而不使用尾随返回类型语法? - Is it possible to return an object of type T by reference from a lambda without using trailing return type syntax? lambda尾随返​​回类型auto的用法是什么? - What is the usage of lambda trailing return type auto? 泛型lambda,继承和尾随返回类型:这是有效的代码吗? - Generic lambda, inheritance and trailing return type: is this valid code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM