我有一种感觉,lambda的类型是一个函数指针.当我进行以下测试时,我发现它是错误的(演示).
#define LAMBDA [] (int i) -> long { return 0; }
int main ()
{
long (*pFptr)(int) = LAMBDA; // ok
auto pAuto = LAMBDA; // ok
assert(typeid(pFptr) == typeid(pAuto)); // assertion fails !
}
Run Code Online (Sandbox Code Playgroud)
上面的代码是否缺少任何一点?如果不是那么,typeof用auto关键字推导出的lambda表达式是什么?
我一直在使用Java 8中的新Optional类型,我遇到了似乎是功能上不支持的常见操作:"orElseOptional"
考虑以下模式:
Optional<Result> resultFromServiceA = serviceA(args);
if (resultFromServiceA.isPresent) return result;
else {
Optional<Result> resultFromServiceB = serviceB(args);
if (resultFromServiceB.isPresent) return resultFromServiceB;
else return serviceC(args);
}
Run Code Online (Sandbox Code Playgroud)
这种模式有很多种形式,但归结为在一个可选项上需要一个"orElse",它接受一个生成一个新的可选项的函数,只有当前的一个不存在时才被调用.
它的实现看起来像这样:
public Optional<T> orElse(Supplier<Optional<? extends T>> otherSupplier) {
return value != null ? this : other.get();
}
Run Code Online (Sandbox Code Playgroud)
我很好奇是否有这样的方法不存在的原因,如果我只是以一种无意的方式使用Optional,以及人们提出了处理这种情况的其他方式.
我应该说,我认为涉及自定义实用程序类/方法的解决方案并不优雅,因为使用我的代码的人不一定知道它们存在.
另外,如果有人知道,这样的方法是否会包含在JDK 9中,我可以在哪里提出这样的方法?对我来说,这似乎是对API的一个相当明显的遗漏.
我是C++ 11的新手.我正在编写以下递归lambda函数,但它不编译.
#include <iostream>
#include <functional>
auto term = [](int a)->int {
return a*a;
};
auto next = [](int a)->int {
return ++a;
};
auto sum = [term,next,&sum](int a, int b)mutable ->int {
if(a>b)
return 0;
else
return term(a) + sum(next(a),b);
};
int main(){
std::cout<<sum(1,10)<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
vimal @ linux-718q:〜/ Study/09C++/c ++ 0x/lambda> g ++ -std = c ++ 0x sum.cpp
sum.cpp:在lambda函数中:sum.cpp:18:36:错误:' ((<lambda(int, int)>*)this)-><lambda(int, int)>::sum'不能用作函数
gcc版本4.5.0 20091231(实验性)(GCC)
但如果我改变sum()下面的声明,它的作用是:
std::function<int(int,int)> sum = [term,next,&sum](int a, …Run Code Online (Sandbox Code Playgroud) 以下代码使用gcc 4.5.1编译,但不使用VS2010 SP1编译:
#include <iostream>
#include <vector>
#include <map>
#include <utility>
#include <set>
#include <algorithm>
using namespace std;
class puzzle
{
vector<vector<int>> grid;
map<int,set<int>> groups;
public:
int member_function();
};
int puzzle::member_function()
{
int i;
for_each(groups.cbegin(),groups.cend(),[grid,&i](pair<int,set<int>> group){
i++;
cout<<i<<endl;
});
}
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
error C3480: 'puzzle::grid': a lambda capture variable must be from an enclosing function scope
warning C4573: the usage of 'puzzle::grid' requires the compiler to capture 'this' but the current default capture mode does …Run Code Online (Sandbox Code Playgroud) 我刚刚开始研究Java 8并试用lambdas我以为我会尝试重写一个我最近编写的非常简单的东西.我需要将String of Map转换为Column到另一个String to Column的Map,其中新Map中的Column是第一个Map中Column的防御副本.列具有复制构造函数.我到目前为止最接近的是:
Map<String, Column> newColumnMap= new HashMap<>();
originalColumnMap.entrySet().stream().forEach(x -> newColumnMap.put(x.getKey(), new Column(x.getValue())));
Run Code Online (Sandbox Code Playgroud)
但我相信必须有一个更好的方法去做,我会感激一些建议.
auto foo = "You're using g++!";
auto compiler_detector = [foo](auto foo) { std::puts(foo); };
compiler_detector("You're using clang++!");
Run Code Online (Sandbox Code Playgroud)
clang ++ 3.6.0和更新的打印出"你正在使用clang ++!" 并警告捕获 foo未被使用.
g ++ 4.9.0和更新版本打印出"你正在使用g ++!" 并警告该参数 foo未被使用.
什么编译器更准确地遵循C++标准?
考虑这个相当无用的程序:
#include <iostream>
int main(int argc, char* argv[]) {
int a = 5;
auto it = [&](auto self) {
return [&](auto b) {
std::cout << (a + b) << std::endl;
return self(self);
};
};
it(it)(4)(6)(42)(77)(999);
}
Run Code Online (Sandbox Code Playgroud)
基本上我们正在尝试制作一个返回自己的lambda.
error: function 'operator()<(lambda at lam.cpp:6:13)>' with deduced return type cannot be used before it is defined
哪个编译器是对的?是否存在静态约束违规,UB或两者都没有?
clang接受更新此轻微修改:
auto it = [&](auto& self, auto b) {
std::cout << (a + b) << std::endl;
return [&](auto p) { return …Run Code Online (Sandbox Code Playgroud) 正如您在下面的代码中看到的,我已将Action<>对象声明为变量.
有人请让我知道为什么这个动作方法委托表现得像一个静态方法?
为什么它会true在以下代码中返回?
public static void Main(string[] args)
{
Action<string> actionMethod = s => { Console.WriteLine("My Name is " + s); };
Console.WriteLine(actionMethod.Method.IsStatic);
Console.Read();
}
Run Code Online (Sandbox Code Playgroud)

采取方法System.Windows.Forms.Control.Invoke(Delegate方法)
为什么会出现编译时错误:
string str = "woop";
Invoke(() => this.Text = str);
// Error: Cannot convert lambda expression to type 'System.Delegate'
// because it is not a delegate type
Run Code Online (Sandbox Code Playgroud)
但这很好用:
string str = "woop";
Invoke((Action)(() => this.Text = str));
Run Code Online (Sandbox Code Playgroud)
当方法需要普通代表时?
当我[=]用来表示我希望所有局部变量都被lambda中的值捕获时,是否会导致被复制的函数中的所有局部变量,或者只是lambda使用的所有局部变量?
所以,例如,如果我有:
vector<int> my_huge_vector(100000);
int my_measly_int;
some_function([=](int i){ return my_measly_int + i; });
Run Code Online (Sandbox Code Playgroud)
my_huge_vector会被复制,即使我不在lambda中使用它吗?