当我尝试编译此代码(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) 我有一些关于从函数返回对局部变量的引用的问题:
class A {
public:
A(int xx)
: x(xx)
{
printf("A::A()\n");
}
};
const A& getA1()
{
A a(5);
return a;
}
A& getA2()
{
A a(5);
return a;
}
A getA3()
{
A a(5);
return a;
}
int main()
{
const A& newA1 = getA1(); //1
A& newA2 = getA2(); //2
A& newA3 = getA3(); //3
}
Run Code Online (Sandbox Code Playgroud)
我的问题是=>
执行getA1()是否正确?我觉得它是错误的,因为它返回局部变量的地址或临时变量.
main(1,2,3)中的哪些陈述会导致未定义的行为?
在const A& newA1 = getA1();做标准的保证,暂时由const引用约束不会被破坏,直到引用超出范围是什么?
我的书说:
具有函数体的Lambda包含除了单个return语句之外的任何未指定返回类型的返回void.
但是这个:
auto f = []{
int i=0; i++;
return std::string("foo");
};
std::cout << f() << std::endl;
Run Code Online (Sandbox Code Playgroud)
实际上编译并打印出"foo",但是lambda expr不仅仅包含一个return语句,因此它应该返回void,因为它不会手动指定" - > std :: string"作为返回类型.
这里发生了什么?
我在基于Clang 3.2的最新Xcode 4.6中使用Apple的编译器似乎:
clang --version
Apple LLVM版本4.2(clang-425.0.24)(基于LLVM 3.2svn)目标:x86_64-apple-darwin12.2.0线程模型:posix
据我所知,在标准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.这里发生了什么?我是否解释了标准错误?
我一直在编写代码,我最近发现g ++没有警告我某类问题:根据C++ 11 5.1.2.4,如果你的lambda不是单个return语句那么返回类型必须声明为尾随返回类型或无效.
虽然g ++被允许编译无效的代码,如果它有足够的意义,是否有办法关闭此行为(允许-fpedantic在g ++ - 4.7中)或至少警告它?
示例代码:
[]() { return 0; } //is fine
[&a]() { a++; return 0; } //is not fine but g++ doesn't warn me
[&a]() -> int {a++; return 0; } //is fine again
Run Code Online (Sandbox Code Playgroud)
C++ 11 5.1.2.4
实现不应将rvalue引用类型的成员添加到闭包类型.如果lambda表达式不包含lambda声明符,那就好像lambda声明符是().如果lambda表达式不包含trailing-return-type,则就好像trailing-return-type表示以下类型:
- 如果compound-statement的格式为
{attribute-specifier-seq(opt)返回表达式; }
返回的表达的左值到右值转换(4.1),阵列到指针转换(4.2),和功能到指针转换(4.3)后的类型;- 否则,无效.
在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)