在下面的代码中,第一次调用foo是不明确的,因此无法编译.
第二个,+在lambda之前添加,解析为函数指针重载.
#include <functional>
void foo(std::function<void()> f) { f(); }
void foo(void (*f)()) { f(); }
int main ()
{
foo( [](){} ); // ambiguous
foo( +[](){} ); // not ambiguous (calls the function pointer overload)
}
Run Code Online (Sandbox Code Playgroud)
+这里的符号是什么?
根据这个答案,我现在想知道lambda的生命周期是什么规则,以及与自动转换创建的函数指针的生命周期有什么关系.关于lambda的生命周期有几个问题(例如这里和这里),在这种情况下,答案是"它们的行为与你自己编写完整的仿函数对象完全一样",但是它们都没有解决转换为函数指针的问题.特殊情况.
我把这个小小的工作实例放在一起,说明了我的担忧:
#include <iostream>
typedef int (*func_t)(int);
// first case
func_t retFun1() {
static auto lambda = [](int) { return 1; };
// automatically converted to func_t
return lambda;
}
// second case
func_t retFun2() {
// no static
auto lambda = [](int) { return 2; };
// automatically converted to func_t and
// the local variable lambda reaches the end of its life
return lambda;
}
int main() {
const int a = …Run Code Online (Sandbox Code Playgroud) 标准中哪些函数不允许返回函数?我知道它们在概念上是荒谬的,但在我看来,语法会允许它们.根据这个网页," noptr-declarator [是]任何有效的声明符 ",它将包含一个函数的声明符:
int f()();
Run Code Online (Sandbox Code Playgroud)
关于语法.
在我看来,[dcl.decl]中拼写的语法允许
int f(char)(double)
Run Code Online (Sandbox Code Playgroud)
这可以解释为功能 f ,需要一个 char 并返回相同的签名功能 int g(double).
1 declarator:
2 ptr-declarator
3 noptr-declarator parameters-and-qualifiers trailing-return-type
4 ptr-declarator:
5 noptr-declarator
6 ptr-operator ptr-declarator
7 noptr-declarator:
8 declarator-id attribute-specifier-seq opt
9 noptr-declarator parameters-and-qualifiers
10 noptr-declarator [ constant-expression opt ] attribute-specifier-seq opt
11 ( ptr-declarator )
12 parameters-and-qualifiers:
13 ( parameter-declaration-clause ) cv-qualifier-seqAfter
Run Code Online (Sandbox Code Playgroud)
粗略地说,在1-> 2,2 = 4,4-> 6,4-> 6后你应该有ptr-operator ptr-operator ptr-operator然后,使用4-> 5,5 = 7,7-> 8第一个宣告者; 对第二和第三个声明符使用4-> 5,5 = 7,7-> …
以下无法在gcc和clang上编译
#include <type_traits>
int foo();
int main()
{
using R = std::result_of_t<decltype(foo)()>; // error
}
Run Code Online (Sandbox Code Playgroud)
两个编译器的错误都是处理声明函数返回函数的非法性.但是我没有声明这样的功能 - 我只是想写出它的类型 - 因为那是result_of预期的.这真的还是不合格吗?
在C++ 11中,lambda函数是一个对象,应该可以用它来调用make_tuple,对吗?
void foobar() {
auto t = std::make_tuple([](){ std::make_shared<int>(); });
}
Run Code Online (Sandbox Code Playgroud)
这段代码适合我.
现在,如果我们添加一个可变参数模板会发生什么:
#include <tuple>
#include <memory>
template <class... T>
void foobar() {
auto t = std::make_tuple([](){ std::make_shared<T>(); }...);
}
int main(int, char**)
{
foobar<int, float, double>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个无法在GCC 4.7.2中编译
main.cpp: In lambda function:
main.cpp:6:54: error: parameter packs not expanded with '...':
main.cpp:6:54: note: 'T'
main.cpp: In function 'void foobar()':
main.cpp:6:57: error: expansion pattern '#'lambda_expr' not supported by dump_expr#<expression error>' contains no argument packs
main.cpp: In instantiation …Run Code Online (Sandbox Code Playgroud) 因此,在Lua中,您可以执行以下操作
local function start(n)
return function()
n = n + 1;
return n;
end;
end
print(start(1)()); --> 2
Run Code Online (Sandbox Code Playgroud)
我听说过模板,但是我不想遇到xy问题,或者还有其他方法