标签: bind

std :: bind是否一般只使用move-only类型,特别是std :: unique_ptr?

我正在尝试使用boost :: asio并遇到一些泥潭.

我正在尝试编译以下代码:

std::unique_ptr<buffer_t> buffer = buffers.pop();
std::function<void(const boost::system::error_code&, size_t)> t = std::bind(&tcp_client::handle_read_done,
                                                                            this,
                                                                            std::placeholders::_1,
                                                                            std::placeholders::_2,
                                                                            std::move(buffer));
Run Code Online (Sandbox Code Playgroud)

如果我排除std :: move(缓冲区),当然从handle_read_done的签名和std :: bind中的传递参数,一切正常.

当试图将它传递给boost :: asio :: async_read_some时,它抱怨从std :: bind返回的对象上隐式删除的函数,对元组上的已删除函数,我假设是因为可移动性,以及大量的提升具体错误.如果我只是尝试将它分配到一个std :: function,它应该与提升调用的签名匹配,我得到那些相同的元组错误,所以我猜它们是相同的.只需将std :: bind的结果赋值给auto就不会产生编译错误,但当然我不能在其上调用任何东西.

我究竟做错了什么?以下是尝试分配时的输出std::function<void(const boost::system::error_code&,size_t)>

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/functional: In static member function ‘static void std::_Function_handler<void(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Functor = std::_Bind<std::_Mem_fn<void (rcon::net::tcp_client::*)(const boost::system::error_code&, long unsigned int, std::unique_ptr<std::array<unsigned char, 10240ul> >)>(rcon::net::tcp_client*, std::_Placeholder<1>, std::_Placeholder<2>, std::unique_ptr<std::array<unsigned char, 10240ul> >)>, _ArgTypes = {const boost::system::error_code&, long unsigned int}]’:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/functional:2148:6:   instantiated from ‘std::function<_Res(_ArgTypes …
Run Code Online (Sandbox Code Playgroud)

c++ bind unique-ptr c++11

11
推荐指数
1
解决办法
5325
查看次数

std :: function - >函数指针

这是一个代码:

#include <functional>
using namespace std::tr1;

typedef void(*fp)(void);

void foo(void)
{

}

void f(fp)
{

}

int main()
{
  function<void(void)> fun = foo;
  f(fun); // error
  f(foo); // ok
}
Run Code Online (Sandbox Code Playgroud)

最初我需要从非静态类方法创建一个函数指针,因为我需要在函数调用之间保存数据.我试过std::tr1::bindboost::bind,但他们返回功能对象,不是指针,正如我所看到的,不能被"铸造"纯功能性的指针.虽然函数signature(SetupIterateCabinet)需要一个纯粹的func指针.

我需要建议如何解决问题.谢谢.

c++ bind functor

11
推荐指数
2
解决办法
1万
查看次数

如何使用Jquery删除附加元素以及为什么绑定或生存会导致元素重复

现在我知道这个基本问题之前已被问过,但我一定做错了.我知道在我可以对它做任何事情之前必须绑定一个附加元素.但是,尝试我可能无法让它工作.

当我们点击收音机选择时,我正在显示一条消息并显示.当我尝试绑定新元素时,它会以奇怪的方式堆叠.它将开始堆叠元素.例如 - [单击1]消息1,[单击2]消息1和2,依此类推.

我尝试了一大堆不同的方法来绑定它.我希望删除会删除#feedback,然后创建并绑定下一条消息.我必须做一些非常错误的事情.我知道这与其他帖子非常相似,但我经历了所有这些帖子并且无法找到足够明确的答案来帮助.先感谢您.

HTML

<div class="answers">
    <ul>
        <li>
            <input id="answer" type="radio" onclick="feedback('THE MESSAGE HTML')"><label>Label</label>
        </li>
    </ul>
</div>
Run Code Online (Sandbox Code Playgroud)

JavaScript的:

function feedback(message)
{
    $('#answer').live('click', function()
    {
        $('#feedback').remove();
    });

    $('#answer').live('click', function()
    {
        $('.answers').append('<div id="feedback">'+message+'</div>');
    });
};
Run Code Online (Sandbox Code Playgroud)

jquery bind append live

11
推荐指数
1
解决办法
9万
查看次数

绑定到特定的IP地址和端口以接收UDP数据

我正在尝试接收由PlayCap(http://www.signal11.us/oss/playcap/)广播到网络地址192.168.103.255端口3000的UDP数据.我遇到了绑定到此地址和端口的问题.这是我的Java代码:

public static void main(String[] args) {
    try {
        DatagramSocket s = new DatagramSocket();
        InetSocketAddress address = new InetSocketAddress("192.168.103.255", 3000);
        s.bind(address);

        byte buffer[] = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        System.out.println("Waiting...");
        s.receive(packet);
        System.out.println("Received!");

    } catch (Exception e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

这会返回错误:

java.net.SocketException: already bound
    at java.net.DatagramSocket.bind(Unknown Source)
    at runner.main(runner.java:12)
Run Code Online (Sandbox Code Playgroud)

我运行了命令"netstat -a -n",输出中的任何地方都没有列出地址192.168.103.255和端口3000,所以我认为这个端口已经没有被使用了.事实上,我尝试的任何地址/端口组合都会出现此错误(包括我的静态IP地址).

我还写了一些C代码来创建一个套接字并绑定到这个地址和端口,但它也在bind调用上失败了.但是,此代码将绑定到我的静态IP地址(192.168.1.149)上的端口.这是代码:

#include <stdio.h>
#include <sys/types.h>
#include <winsock.h>
#include <unistd.h>

#define a1 192
#define a2 168
#define a3 103
#define a4 255
#define PORT …
Run Code Online (Sandbox Code Playgroud)

c java udp bind

11
推荐指数
1
解决办法
3万
查看次数

JavaFX:使用常量字符串前缀绑定StringProperty

我对JavaFX中的绑定功能有疑问.我想要的是绑定2个字符串属性.但他们的价值观不应该相等.

让我举一个例子:

我有一个StringProperty代表我的应用程序中最后打开的项目.
值类似于"C:\ temp\myProject.prj".
我想在窗口标题中显示此路径.
这很简单:stage.titleProperty().bind(lastprojectProperty());
但我不想只显示项目路径,还要显示应用程序名称,
例如:MyApplication 2.2.4 - C:\ temp\myProject.prj.

可以使用绑定并添加一个常量前缀字符串吗?或者我是否使用ChangeListerner?

使用ChangeListener的解决方案存在初始值的问题......

    final StringProperty path = new SimpleStringProperty("untitled");
    final StringProperty title = new SimpleStringProperty("App 2.0.0");

    path.addListener(new ChangeListener<String>()
  {
        @Override
        public void changed(ObservableValue<? extends String> ov, String t, String newValue)   
        {
            title.setValue("App 2.0.0 - " + newValue);
        }
  });                

    // My title shows "App 2.0.0" since there is now change event throws until now...
    // Of course I could call path.setValue("untitled"); 
    // And above path = new …
Run Code Online (Sandbox Code Playgroud)

bind javafx properties changelistener

11
推荐指数
1
解决办法
1万
查看次数

如何避免在每个函数上绑定(this)?

我正在实现一个构建在OpenLayers3之上的Web地图客户端,它应该能够连接到多个WMS服务器,请求WMS功能并显示服务器通告的层.

var MyMapClient = function(params) {
    this.wms_sources_ = params.wms_sources;
    this.wms_capabilities_ = [];
}

MyMapClient.prototype.parse_capabilities = function(index) {
    var capabilities = this.wms_capabilities_[index];
    // do something with capabilities 
}

MyMapClient.prototype.load_wms_capabilities = function() {
    var parser = new ol.format.WMSCapabilities();

    jQuery.each(this.wms_sources_, (function (index, wms_source) {

        console.log("Parsing " + wms_source.capabilities_url);

        jQuery.when(jQuery.ajax({
            url: wms_source.capabilities_url,
            type: "GET",
            crossDomain: true,
        })).then((function (response, status, jqXHR) {
            var result = parser.read(response);
            console.log("Parsed Capabilities, version " + result.version);
            this.wms_capabilities_[index] = result;
            return index;
        }).bind(this)).then(this.parse_capabilities.bind(this));

    }).bind(this));
};
Run Code Online (Sandbox Code Playgroud)

上面的代码运行正常,但我bind(this)每次都想调用一个需要访问MyMapClient实例的"私有"变量的函数.是否有更好的方法可以一致地访问实例内部,而不会牺牲可读性?

javascript bind

11
推荐指数
1
解决办法
1040
查看次数

为什么Butterknife @Bind在发布版本中失败(在proguard之后)

我一直在使用Butterknife构建Android应用程序,最近升级到7.0.1.我用新功能替换了所有@InjectViewButterKnife.inject使用,@Bind并且没有调试版本的问题,但应用程序在启动时崩溃发布版本.

如果我minifyEnabled在build.gradle中将' ' 切换为false,那么我可以生成一个有效的发布版本.

我正在使用Butterknife网站上记录的proguard配置,但它似乎对我不起作用.我也在构建中使用Dagger,Picasso和Flurry.

我的proguard-rules.pro内容:

# ButterKnife
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }

-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}

# Dagger
-keepclassmembers,allowobfuscation class * {
    @javax.inject.* *;
    @dagger.* *;
    <init>();
}

-keep class javax.inject.** { *; }
-keep class **$$ModuleAdapter
-keep class **$$InjectAdapter
-keep class **$$StaticInjection
-keep class dagger.** { *; }

# Picaso
-dontwarn …
Run Code Online (Sandbox Code Playgroud)

android bind proguard butterknife

11
推荐指数
2
解决办法
3414
查看次数

替换std :: bind2nd

我有一个foostd::vector<int>.它表示一组范围的"边缘"值.

例如,如果foo是{1,3,5,7,11},那么范围是1-3,3-5,5-7,7-11.对我而言,这相当于4个时期.请注意,每个句点包括范围中的第一个数字,而不是最后一个数字.所以在我的例子中,8出现在第3个(从零开始)的时期.7也出现在第3期.11以上不会出现在任何地方.2出现在第0期.

鉴于bar哪个是int,我使用

std::find_if(
    foo.begin(),
    foo.end(),
    std::bind2nd(std::greater<int>(), bar)
) - foo().begin() - 1;
Run Code Online (Sandbox Code Playgroud)

给我应该包含的时期bar.

我的问题:std::bind2nd已被弃用,所以我应该重构.使用更新函数的等效语句是什么?std::bind不会以明显的方式"堕入".

c++ bind bind2nd c++11

11
推荐指数
3
解决办法
4606
查看次数

如何在Docker容器中挂载-bind?

我有这个容器基于debian:jessie(但这不是很相关,因为我有同样的问题alpine:3.3).我达到了我需要的地步

mount --bind /htdocs/www /home/user/example.com/www
Run Code Online (Sandbox Code Playgroud)

我明白了

mount: permission denied
Run Code Online (Sandbox Code Playgroud)

我在任何内核日志中都找不到任何东西,并且-vvv没有任何有趣的东西.我显然可以在主机上执行此操作(使用任何其他子树/节点对).在上面的示例中,/ htdocs/www是Docker卷的挂载点,但它看起来并不重要,因为我不能mount --bind在容器内部任何子树/节点对.

mount bind docker

11
推荐指数
2
解决办法
8435
查看次数

function.apply.bind如何在以下代码中工作?

所以我得到了一个[200,599]的数组从promise返回,并且spread内的回调函数被传递给Function.apply.bind,但现在我迷路了.如何将[200,599]的数组分成x和y?apply.bind究竟是如何工作的?

function getY(x) {
        return new Promise( function(resolve,reject){
            setTimeout( function(){
                resolve( (3 * x) - 1 );
            }, 100 );
        } );
    }

function foo(bar,baz) {
    var x = bar * baz;

    // return both promises
    return [
        Promise.resolve( x ),
        getY( x )
    ];
}

function spread(fn) {
    return Function.apply.bind( fn, null );
}

Promise.all(
    foo( 10, 20 )
)
.then(
    spread( function(x,y){
        console.log( x, y );    // 200 599
    } )
)
Run Code Online (Sandbox Code Playgroud)

javascript bind apply promise ecmascript-6

11
推荐指数
3
解决办法
1180
查看次数