相关疑难解决方法(0)

Lambda的显式返回类型

当我尝试编译此代码(VS2010)时,我收到以下错误: error C3499: a lambda that has been specified to have a void return type cannot return a value

void DataFile::removeComments()
{
  string::const_iterator start, end;
  boost::regex expression("^\\s?#");
  boost::match_results<std::string::const_iterator> what;
  boost::match_flag_type flags = boost::match_default;
  // Look for lines that either start with a hash (#)
  // or have nothing but white-space preceeding the hash symbol
  remove_if(rawLines.begin(), rawLines.end(), [&expression, &start, &end, &what, &flags](const string& line)
  {
    start = line.begin();
    end = line.end();
    bool temp = boost::regex_search(start, end, what, expression, flags);
    return temp; …
Run Code Online (Sandbox Code Playgroud)

c++ lambda visual-c++ c++11 visual-c++-2012

74
推荐指数
3
解决办法
9万
查看次数

什么时候可以省略C++ 11 lambda中的返回类型?

据我所知,在标准C++ 11(不是C++ 14)中,当省略lambda的返回类型时,其返回类型推断为:

  1. 返回表达式的类型,只要lambda只包含带有表达式的单个return语句,或者
  2. void 在所有其他情况下.

现在考虑这段代码:

#include <iostream>

auto closure = [](int x)
{
    x++;
    return x;
};

int main()
{
    int y = closure(10);
    std::cout << y << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

这应该属于案例2.但是代码编译好像是带有auto类型推导的C++ 14 ,在g ++ 4.9.2,g ++ 5和clang ++中都有-pedantic -Wall -Wextra -std=c++11.这里发生了什么?我是否解释了标准错误?

c++ lambda c++11 type-deduction

13
推荐指数
2
解决办法
1290
查看次数

为什么lambda函数默认情况下会删除推断的返回类型引用?

在C++ 14中,为什么lambda函数带有推导的返回类型默认情况下从返回类型中删除引用?IIUC,因为C++ 14 lambda函数具有推导的返回类型(没有显式的尾随返回类型)具有返回类型auto,它会丢弃引用(以及其他内容).

为什么做出这个决定?在我看来,当你的返回语句返回时,就像删除引用一样.

这种行为给我带来了以下令人讨厌的错误:

class Int {
public:
   Int(int i) : m_int{i} {}
   int m_int;
};

class C {
public:
    C(Int obj) : m_obj{obj} {}
    const auto& getObj() { return m_obj; }
    Int m_obj;
};

class D {
public:
    D(std::function<const Int&()> f) : m_f{f} {}
    std::function<const Int&()> m_f;
};

Int myint{5};
C c{myint};
D d{ [&c](){ return c.getObj(); } } // The deduced return type of the lambda is Int (with no reference)
const Int& myref …
Run Code Online (Sandbox Code Playgroud)

c++ lambda clang auto c++14

5
推荐指数
1
解决办法
809
查看次数

Lambda没有自动推断出返回类型

当我在/sf/answers/2248084891/上回答我自己的问题时,我又有了疑问.

const CArray<CItem*>& Items=
    (ItemsInput!= nullptr)?
        *ItemsInput
    :
        [this]() -> const CArray<CItem*>&
        {
            CArray<CItem*> InnerItems;
            GetContainer().GetInnerItems(InnerItems, NULL, true);
            return (InnerItems);
        } ()
;
Run Code Online (Sandbox Code Playgroud)

我试图删除-> const CArray<CItem*>&返回部分,但在编译时它给出了两个错误:

1>FunctionClass.cpp(line of last semicolon): error C2440: 'initializing' : cannot convert from 'void' to 'const CArray<TYPE> &'
1>          with
1>          [
1>              TYPE=CItem *
1>          ]
1>          Expressions of type void cannot be converted to other types


1>FunctionClass.cpp(line of the return statement): error C3499: a lambda that has been specified …
Run Code Online (Sandbox Code Playgroud)

c++ lambda return-type type-deduction

3
推荐指数
1
解决办法
350
查看次数