1

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"

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? This will work in VS2013?

2
  • It seems correct, did you try to specify return type -> bool as workaround ? Commented Jul 24, 2015 at 11:20
  • I have just edited the code. will it work in VS2013? Commented Jul 24, 2015 at 11:30

1 Answer 1

6

Problem is, that you have lambda with two lines and compiler cannot determine return type (so it's equal to void) in C++11. You can specify 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.

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);
});
Sign up to request clarification or add additional context in comments.

1 Comment

@LightnessRacesinOrbit cppreference.com says, that The return type is deduced from return statements as if for a function whose return type is declared auto. since C++14.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.