简体   繁体   English

lambda函数抛出错误

[英]lambda function throwing error

I am getting this error message when I build my code, 构建代码时出现此错误消息,

"a lambda that has been specified to have a void return type cannot return a value" “被指定为具有无效返回类型的lambda无法返回值”

bool StockCheck::InStock(const Shop& shop) const
{
    return std::any_of(m_products, [&shop, this](const std::unique_ptr<SelectedProduct>& selected)
    {
        auto inStock = selected->ProductInStock(shop);
        return inStock != SelectedProduct::NOT_IN_STOCK && selected->GetProductInStock(code);
    });
}

I am using VS2010, is it a problem? 我正在使用VS2010,有问题吗? This will work in VS2013? 这将在VS2013中工作吗?

Problem is, that you have lambda with two lines and compiler cannot determine return type (so it's equal to void) in C++11. 问题是,您有两行的lambda,并且编译器无法确定C ++ 11中的返回类型(因此等于void)。 You can specify ret. 您可以指定ret。 type manually like 像手动输入

return std::any_of(m_products.begin(), m_products.end(),
[&shop, this](const std::unique_ptr<SelectedProduct>& selected) -> bool
{
    auto inStock = selected->ProductInStock(shop);
    return inStock != SelectedProduct::NOT_IN_STOCK && selected->GetProductInStock(code);
});

or write without variable inStock just in one line. 或只写一行而没有变量inStock

return std::any_of(m_products.begin(), m_products.end(),
[&shop, this](const std::unique_ptr<SelectedProduct>& selected)
{
    return selected->ProductInStock(shop) != SelectedProduct::NOT_IN_STOCK &&
    selected->GetProductInStock(code);
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM