基本上,我希望能够做的是使用任意数量的任何类型的参数lambda并将其转换为std :: function.我尝试过以下方法,两种方法都不起作用.
std::function([](){});//Complains that std::function is missing template parameters
template <typename T> void foo(function<T> f){}
foo([](){});//Complains that it cannot find a matching candidate
Run Code Online (Sandbox Code Playgroud)
但是,下面的代码确实有效,但它不是我想要的,因为它需要明确说明对通用代码不起作用的模板参数.
std::function<void()>([](){});
Run Code Online (Sandbox Code Playgroud)
我整个晚上一直在使用功能和模板,我只是想不出来,所以任何帮助都会非常感激.
正如评论中所提到的,我试图这样做的原因是因为我试图使用可变参数模板在C++中实现currying.不幸的是,这在使用lambdas时非常糟糕.例如,我可以使用函数指针传递标准函数.
template <typename R, typename...A>
void foo(R (*f)(A...)) {}
void bar() {}
int main() {
foo(bar);
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何将lambda传递给这样的可变函数.为什么我对将泛型lambda转换为std :: function感兴趣是因为我可以执行以下操作,但最终要求我明确地将模板参数声明为std :: function,这正是我想要避免的.
template <typename R, typename...A>
void foo(std::function<R(A...)>) {}
int main() {
foo(std::function<void()>([](){}));
}
Run Code Online (Sandbox Code Playgroud) 我正在使用.NET4.5和VS2013,我有这个查询dynamic从db 获得结果.
dynamic topAgents = this._dataContext.Sql(
"select t.create_user_id as \"User\", sum(t.netamount) as \"Amount\" from transactiondetail t where t.update_date > sysdate -7 group by t.create_user_id")
.QueryMany<dynamic>();
Run Code Online (Sandbox Code Playgroud)
以下语句失败并出现编译错误Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
,甚至不允许我运行它
topAgents.ToList().Select(agent => new
{
User = agent.User != null ? string.Format("{0}", agent.User).Replace("CORPNTGB\\", "") : null,
Amount = agent.Amount
});
Run Code Online (Sandbox Code Playgroud)
虽然这个foreach工作得很好.
var data = new List<List<object>>(); …Run Code Online (Sandbox Code Playgroud) 允许var在Java 10中分配如下字符串:
var foo = "boo";
Run Code Online (Sandbox Code Playgroud)
虽然不允许为其分配lambda表达式,例如:
var predicateVar = apple -> apple.getColor().equals("red");
Run Code Online (Sandbox Code Playgroud)
为什么不能推断的λ或方法引用类型时,它可以推断出休息等String,ArrayList,用户类等?
我的问题是关于InterruptedException,它是从Thread.sleep方法中抛出的。在合作时,ExecutorService我注意到一些我不理解的怪异行为;这是我的意思:
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
while(true)
{
//DO SOMETHING
Thread.sleep(5000);
}
});
Run Code Online (Sandbox Code Playgroud)
有了这个代码,编译器不给我任何错误或消息InterruptedException从Thread.sleep应该被抓。但是,当我尝试更改循环条件并用诸如此类的变量替换“ true”时:
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
while(tasksObserving)
{
//DO SOMETHING
Thread.sleep(5000);
}
});
Run Code Online (Sandbox Code Playgroud)
编译器不断抱怨InterruptedException必须处理。有人可以向我解释为什么会发生这种情况,为什么将条件设置为true则编译器会忽略InterruptedException?
在搜索列表时是否有一个linq lambda搜索方法返回null,而不是抛出异常?
我目前的解决方案是这样的:(以避免被抛出异常)
if (list.Exists(x => x.Foo == Foo))
{
var listItem = list.Find(x => x.Foo == Foo);
}
Run Code Online (Sandbox Code Playgroud)
重复表达只是感觉不对.
就像是 ...
var listItem = list.Find(x => x.Foo == Foo);
if (listItem != null)
{
//Do stuff
}
Run Code Online (Sandbox Code Playgroud)
......对我来说感觉更好 还是只是我?
你有更好的方法吗?(解决方案不必返回null,只是更好的解决方案是好的)
当我写一个lambda时[=],是否意味着我所有的局部变量都将被复制到创建的struct的成员中,或者我可以假设只有那些将在lambda中实际使用的那些?例如:
void f()
{
vector<int> v(10000);
const int n = 5;
const int DivByNCnt = count_if(istream_iterator<int>(cin), istream_iterator<int>(),
[=](int i)
{
return i % n == 0;
});
}
Run Code Online (Sandbox Code Playgroud)
如果有的话,以下哪一项是正确的?
假设为了论证,vector的复制构造函数有副作用.
是否可以使用C#在方法调用中将lambda表达式作为IComparer参数传递?
例如
var x = someIEnumerable.OrderBy(aClass e => e.someProperty,
(aClass x, aClass y) =>
x.someProperty > y.SomeProperty ? 1 : x.someProperty < y.SomeProperty ? -1 : 0);
Run Code Online (Sandbox Code Playgroud)
我不能完全把它编译成所以我猜不是,但看起来lambda和匿名代表之间的这种明显的协同作用让我觉得我必须做一些愚蠢的错误.
TIA
例:
#include <functional>
int main() {
auto test = []{};
test = []{};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这会在gcc 4.7.2中发出以下错误消息:
test.cpp: In function ‘int main()’:
test.cpp:5:13: error: no match for ‘operator=’ in ‘test = <lambda closure object>main()::<lambda()>{}’
test.cpp:5:13: note: candidate is:
test.cpp:4:16: note: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
test.cpp:4:16: note: no known conversion for argument 1 from ‘main()::<lambda()>’ to ‘const main()::<lambda()>&’
Run Code Online (Sandbox Code Playgroud)
从标准5.1.2.3(强调我的):
实现可以定义闭包类型与下面描述的不同,前提是这不会改变程序的可观察行为,只需更改:
- 封闭类型的大小和/或对齐方式,
- 封闭类型是否可以轻易复制(第9条)
- 闭包类型是否为标准布局类(第9条),或
- 闭包类型是否为POD类(第9条).
据我所知,这就是我遇到的情况.它试图使用已删除的赋值运算符并失败.我很想知道是否有一个简单的解决方法,更广泛地说,一般来说,为lambdas省略复制可构造性的动机理由是什么.
我正在阅读Lambda:Libraries Edition的状态,我对一个声明感到惊讶:
在Streams部分下,有以下内容:
List<Shape> blue = shapes.stream()
.filter(s -> s.getColor() == BLUE)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
该文件没有说明shapes实际是什么,我不知道它是否重要.
令我困惑的是:List这段代码返回了什么样的具体内容?
List<Shape>,这是完全正常的.stream()也filter()决定使用哪种列表.Collectors.toList()既没有指定具体的类型List.那么,这里使用的具体类型(子类)List是什么?有保证吗?
以下代码令人惊讶地成功编译:
Consumer<String> p = ""::equals;
Run Code Online (Sandbox Code Playgroud)
这个也是:
p = s -> "".equals(s);
Run Code Online (Sandbox Code Playgroud)
但是这会因boolean cannot be converted to void预期错误而失败:
p = s -> true;
Run Code Online (Sandbox Code Playgroud)
使用括号修改第二个示例也会失败:
p = s -> ("".equals(s));
Run Code Online (Sandbox Code Playgroud)
它是Java编译器中的错误还是我不知道的类型推断规则?