C ++ 11和缺乏多态lambdas - 为什么? [英] C++11 and the lack of polymorphic lambdas - why?

查看:109
本文介绍了C ++ 11和缺乏多态lambdas - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在审查 C ++ 11 标准的草稿版本。特别是关于 lambdas 的部分,我对不引入多态lambda语言的推理感到困惑。 / p>

例如,在100001种方式中,可以使用多态lambdas,我希望我们可以使用如下代码:

  template< typename Container> 
void foo(Container c)
{
for_each(c.begin(),c.end(),[](T& t){++ t;});
}

原因是什么:


$ b $


  • 这种多态性lambda是太难以实现的


  • 或者也许他们被视为 PTB




  • 注意:不是唯一的,它只是作为指南的类型的代码。



    相关来源:








    b

    解决方案

    我们没有多态性lambdas的原因在此发布中得到了很好的解释。



    它与从C ++ 11中提取的概念功能有关:实质上,多态lambda是普通的,无约束的函数模板,我们不知道如何类型检查概念 - 使用无约束模板的约束模板。然而,解决这个问题很容易,如这里所示,所以我不要认为还有任何障碍。


    I've been reviewing the draft version of the C++11 standard. Specifically the section on lambdas, and I am confused as to the reasoning for not introducing polymorphic lambdas.

    For example, amongst the 100001 ways polymorphic lambdas could be used, I had hoped we could use code such as the following:

    template<typename Container>
    void foo(Container c)
    {
        for_each(c.begin(), c.end(), [](T& t) { ++t; });
    }
    

    What were the reasons:

    • Was it that the committee ran out of time?

    • That polymorphic lambdas are too hard to implement?

    • Or perhaps that they are seen as not being needed by the PTB?

    Note: Please remember the example above is not the only one, and it is only provided as a guide to the types of code. Answers that solely concentrate on providing a workaround for the above piece of code will not be considered as valid!

    Related sources:

    解决方案

    The reason we don't have polymorphic lambdas is explained pretty well in this posting.

    It has to do with the concepts feature that was pulled from C++11: essentially, polymorphic lambdas are ordinary, unconstrained function templates and we didn't know how to typecheck a concept-constrained template that used an unconstrained template. However, solving that problem turns out to be easy as shown here, so I don't think there's any obstacle remaining.

    这篇关于C ++ 11和缺乏多态lambdas - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆