当我尝试编译此代码(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++ 11(不是C++ 14)中,当省略lambda的返回类型时,其返回类型推断为:
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++ 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) 当我在/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)