【问题标题】:std::max with lambda and auto带有 lambda 和 auto 的 std::max
【发布时间】:2014-03-23 13:44:00
【问题描述】:

难道这不能用 C++11 实现吗?
使用当前的 clang 编译器(OS X 10.8 上的 Xcode 5)编译失败:

std::max_element(group->GetComponents().begin(), group->GetComponents().end(),
                 [](auto a, auto b) { return a.length > b.length; });

错误信息是: Stuff.cp:68:40: 'auto' 不允许在函数原型中

【问题讨论】:

标签: c++ c++11 lambda auto


【解决方案1】:

在 C++1y 中,您有通用 lambda,因此语法将在 clang 3.5 中编译。 lambda 将如下所示:

class /* unnamed */
{
public:
    template<typename T>
    T operator () (T a) const { return a; }
};

【讨论】:

    猜你喜欢
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2017-04-18
    • 1970-01-01
    • 2017-02-20
    相关资源
    最近更新 更多