我在检测为什么这不是编译时遇到了麻烦.我有一些std::function基于某些参数返回的lambda函数.
我已经将我的问题缩小到这个片段(它不使用lambdas,但完全重现了我的错误):
#include <functional>
#include <iostream>
struct foo {
template<class T>
void bar(T data) {
std::cout << data << "\n";
}
};
void some_fun(const std::function<void(int)> &f) {
f(12);
}
int main() {
foo x;
auto f = std::bind(&foo::bar<int>, x, std::placeholders::_1);
auto w = std::bind(some_fun, f);
w();
}
Run Code Online (Sandbox Code Playgroud)
调用w()产生一个可爱的gcc错误输出,其中我无法弄清楚出了什么问题.这是gcc 4.6.1回应的错误:
g++ -std=c++0x test.cpp -o test
test.cpp: In function ‘int main()’:
test.cpp:20:7: error: no match for call to ‘(std::_Bind<void (*(std::_Bind<std::_Mem_fn<void (foo::*)(int)>(foo, std::_Placeholder<1>)>))(const std::function<void(int)>&)>) ()’
/usr/include/c++/4.6/functional:1130:11: note: candidates are: …Run Code Online (Sandbox Code Playgroud) 除了更改路由表之外,Windows中是否有可靠的方法来强制新创建的套接字使用特定的网络接口?据我所知,bind()接口的IP地址并不能保证这一点.
我使用这个得到一个编译错误:
std::vector<std::function<int(int)>> functions;
std::function<int(int, int)> foo = [](int a, int b){ return a + b; };
std::function<int(int)> bar = std::bind(foo, 2);
functions.push_back(bar);
Run Code Online (Sandbox Code Playgroud)
错误是:
/usr/include/c++/4.6/functional:1764:40:错误:无法调用'(std :: _ Bind(int)>)(int)'
有人能告诉我如何将a std::bind转换为std::function?
我很生气 - 也许有人能够帮助我.
我需要在AJAX调用之后将点击重新绑定到链接,但由于某种原因它不想工作.
这是我的代码:
if ($('.active').length > 0) {
$('.active').click(function() {
var elem = $(this);
var url = $(this).attr('href');
$.ajax({
url: url,
dataType: 'html',
success: function(data) {
elem.replaceWith(data);
}
});
$('.active').bind('click'); return false;
});
}
Run Code Online (Sandbox Code Playgroud)
任何的想法?
感谢您的回复 - 我修改了代码,但问题仍然存在:
function makeActive() {
if ($('.active').length > 0) {
$('.active').click(function() {
var elem = $(this);
var url = $(this).attr('href');
$.ajax({
url: url,
dataType: 'html',
success: function(data) {
elem.replaceWith(data);
}
});
$('.active').live('click', makeActive);
return false;
});
}
}
$('.active').live('click', makeActive);
Run Code Online (Sandbox Code Playgroud) boost :: bind在很多情况下非常方便.其中之一是调度/发布一个方法调用,以便io_service稍后可以进行调用.
在这种情况下,boost :: bind的行为就像人们可能坦率地期望的那样:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
boost::asio::io_service ioService;
class A {
public: A() {
// ioService will call B, which is private, how?
ioService.post(boost::bind(&A::B, this));
}
private: void B() {}
};
void main()
{
A::A();
boost::asio::io_service::work work(ioService);
ioService.run();
}
Run Code Online (Sandbox Code Playgroud)
但是,据我所知,boost创建了一个functor(一个带有operator()()的类,能够在给定对象上调用给定的方法.该班级是否可以访问私人B?我猜不会.
我在这里错过了什么?
我正在尝试使用"空"配置文件启动haproxy(版本1.5.8 2014/10/31),我得到:
user@server:~$ sudo service haproxy start
[....] Starting haproxy: haproxy[ALERT] 126/120540 (7363) : Starting frontend GLOBAL: cannot bind UNIX socket [/run/haproxy/admin.sock]
Run Code Online (Sandbox Code Playgroud)
尽管它已启用:
user@server:~$ cat /etc/default/haproxy
# Set ENABLED to 1 if you want the init script to start haproxy.
ENABLED=1
Run Code Online (Sandbox Code Playgroud)
配置文件:
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
# Default ciphers to …Run Code Online (Sandbox Code Playgroud) 我有一个包含以下模板的父组件:
<ion-content>
<blocks-banners-slideshow class="contentBlock" [config]="{ zone: 'mobile'}" [config_private]="{ url: 'url'}" [slideOptions]="{ loop: true, pager: true}"></blocks-banners-slideshow>
<blocks-catalog-category class="contentBlock" [config]="{ parent_id: 0 }" [config_private]="{ url: 'url'}"></blocks-catalog-category>
<blocks-catalog-topproducts class="contentBlock" [config]="{ filter: { dir: 204}, page: 1, pageSize: 8}" [config_private]="{ url: 'url', showMoreProducts: false, columns: { tablet: 4, phone: 2}}"></blocks-catalog-topproducts>
</ion-content>
Run Code Online (Sandbox Code Playgroud)
然后应用程序尝试显示它告诉我:未处理的承诺拒绝:
模板解析错误:
Can't bind to 'config_private' since it isn't a known property of 'blocks-banners-slideshow'.
1. If 'blocks-banners-slideshow' is an Angular component and it has 'config_private' input, then verify that it is part of …Run Code Online (Sandbox Code Playgroud) 在我的.aspx中,我希望根据来自绑定的值添加一个If语句.我尝试过以下方法:
<% if(bool.Parse(Eval("IsLinkable") as string)){ %>
monkeys!!!!!!
(please be aware there will be no monkeys,
this is only for humour purposes)
<%} %>
Run Code Online (Sandbox Code Playgroud)
IsLinkable是来自Binder的bool.我收到以下错误:
InvalidOperationException
Databinding methods such as Eval(), XPath(), and Bind() can only
be used in the context of a databound control.
Run Code Online (Sandbox Code Playgroud) 有没有办法将std :: bind绑定到std :: weak_ptr?我想存储一个"弱函数"回调,当被调用者被销毁时,它会自动"断开连接".
我知道如何使用shared_ptr创建一个std :: function:
std::function<void()> MyClass::GetCallback()
{
return std::function<void()>(std::bind(&MyClass::CallbackFunc, shared_from_this()));
}
Run Code Online (Sandbox Code Playgroud)
但是返回的std :: function使我的对象永远保持活着.所以我想将它绑定到weak_ptr:
std::function<void()> MyClass::GetCallback()
{
std::weak_ptr<MyClass> thisWeakPtr(shared_from_this());
return std::function<void()>(std::bind(&MyClass::CallbackFunc, thisWeakPtr));
}
Run Code Online (Sandbox Code Playgroud)
但那不编译.(std :: bind将不接受weak_ptr!)有没有办法绑定到weak_ptr?
我已经找到了关于这个的讨论(见下文),但似乎没有标准的实现.存储"弱功能"的最佳解决方案是什么,特别是如果Boost不可用?
讨论/研究(所有这些都使用Boost并且没有标准化):
我正在使用Angular,我在做一些我通常使用jQuery的东西时遇到了麻烦.
我想将click事件绑定到一个元素,然后单击,向下和向上滑动一个兄弟元素.
这就是jQuery的样子:
$('element').click(function() {
$(this).siblings('element').slideToggle();
});
Run Code Online (Sandbox Code Playgroud)
使用Angular我在我的标记中添加了一个带有函数的ng-click属性:
<div ng-click="events.displaySibling()"></div>
Run Code Online (Sandbox Code Playgroud)
这就是我的控制器的样子:
app.controller('myController', ['$scope', function($scope) {
$scope.events = {};
$scope.events.displaySibling = function() {
console.log('clicked');
}
}]);
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是按预期工作,但我不知道如何完成幻灯片.很感谢任何形式的帮助.
我用指令取代了我所拥有的东西.
我的标记现在看起来像这样:
<div class="wrapper padding myevent"></div>
Run Code Online (Sandbox Code Playgroud)
我删除了控制器中的内容并创建了新指令.
app.directive('myevent', function() {
return {
restrict: 'C',
link: function(scope, element, attrs) {
element.bind('click', function($event) {
element.parent().children('ul').slideToggle();
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
但是,我仍然无法让幻灯片切换工作.我不相信Angular支持slideToggle().有什么建议?
bind ×10
c++ ×4
c++11 ×2
function ×2
sockets ×2
angular ×1
angularjs ×1
behavior ×1
binding ×1
boost ×1
c# ×1
callback ×1
eval ×1
events ×1
functor ×1
haproxy ×1
if-statement ×1
input ×1
jquery ×1
networking ×1
properties ×1
slide ×1
startup ×1
std ×1
std-function ×1
typescript ×1
weak-ptr ×1
webforms ×1
windows ×1